Selection&merge Sort
Selection&merge Sort
SORTING
In first iteration, the array is scanned from the first to the last element
and the element that has the smallest is selected. The value of the
selected smallest element is interchanged with the first element of the
array
In second iteration, the array is scanned from second to the last element
and the element that has smallest value is selected. The value of
smallest element is interchanged with the second element of the array
4 19 1 3
4 19 1 3
Iteration-1
The array is scanned starting from the first to the last element and
the element that has the smallest value is selected. The smallest
value is 1 at location 3. The address of element that has the smallest
value is noted and the selected value is interchanged with the first
element i.e.
A[1] and A[3] are swapped
1 19 4 3
SELECTION SORT
1 19 4 3
Iteration-2
The array is scanned starting from the second to the last element and
the element that has the smallest value is selected. The smallest
value is 3 at location 4. The address of element that has the smallest
value is noted. The selected value is interchanged with the second
element i.e.
A[2] and A[4] are swapped
1 3 4 19
SELECTION SORT
1 3 4 19
Iteration-3
The array is scanned starting from the third to the last element and
the element that has the smallest value is selected. The smallest
value is 4 at location 3. The address of element that has the smallest
value is noted. The selected value is interchanged with the third
element i.e.
A[3] and A[3] are swapped
1 3 4 19
ALGORITHM – SELECTION SORT
SelectionSort()
Algorithm to sort an array A consisting of N elements in
ascending order
1. Start
2. Set U = 1
5. Set Loc = U
6. Set I = U + 1
10. T = A[Loc]
A[Loc] = A[U]
A[U] = T
11. U = U + 1
12. Exit
MERGE SORT
MERGE SORT
It splits the list to be sorted into two equal halves and
places them in separate array
If the lower bound of the array is LB and upper bound is UB, then
the midpoint of array is:
MID = (LB + UB) / 2
16 17 2 8 18 1
A B
16 17 2 8 18 1
Compare A[1] to B[1], so B[1] is less than A[1], the value of B[1] is
move to AB[1]
AB
1
MERGE SORT
A B
2 16 17 1 8 18
Compare A[1] to B[2], so A[1] is less than B[2], the value of A[2] is
move to AB[2]
AB
1 2
MERGE SORT
A B
2 16 17 1 8 18
Compare A[2] to B[2], so B[2] is less than A[2], the value of B[2] is
move to AB[3]
AB
1 2 8
MERGE SORT
A B
2 16 17 1 8 18
Compare A[2] to B[3], so A[2] is less than B[3], the value of A[2] is
move to AB[4]
AB
1 2 8 16
MERGE SORT
A B
2 16 17 1 8 18
Compare A[3] to B[3], so A[3] is less than B[3], the value of A[3] is
move to AB[5]
AB
1 2 8 16 17
1 2 8 16 17 18
ALGORITHM – MERGE SORT
MergeSort()
Algorithm to sort an array AB consisting of N elements
1. Start
2. Divide array AB into two sub-arrays A & B
3. Set Mid = (N + 1) / 2
4. Repeat step-5 For I = 1 to Mid by 1
5. A[I] = AB[I]
6. Repeat step-7 For I = Mid + 1 to N by 1
7. B[I-Mid] = AB[I]
8. Set R = Mid
9. Set S = N - Mid
10. Sort A & B
ALGORITHM – MERGE SORT
11. Set L1 = 1
12. Set L2 = 1
13. Set L = 1
14. Repeat Step-15 While (L1 <= R and L2 <= S)
15. If A[L1] < B[L2] then
AB[L] = A[L1]
L=L+1
L1 = L1 + 1
Else
AB[L] = B[L2]
L=L+1
L2 = L2 + 1
End if
ALGORITHM – MERGE SORT
16. If L1 > R then
Repeat For I = 0 to S - L2
AB[L+I] = B[L2+I]
Else
Repeat For I = 0 to R - L1
AB[L+I] = A[L1+I]
End if
17. Exit
Keep Calm
It is the
End of My
Presentation