Sorting plays a very important role in data structure.
Different type of sorting is given that is most popular and mostly used. It is described in very easy and brief way.
4. Divide the problems into a number of sub
problems.
Conquer the sub problems by solving
them recursively. If the sub-problem
sizes are small enough, just solve the
problems in a straight forward manner.
Combine the solutions to the sub
problems into the solution for the original
problem.
Divide and Conquer Approach
5. Divide the n element sequence to be
sorted into two subsequences of n/2
elements each.
Conquer: Sort the two subsequences to
produce the sorted answer.
Combine: Merge the two sorted sub
sequences to produce the sorted answer.
Merge Sort
7. Merge Sort Algorithm
MergeSort(A, i, j)
if j > i then
mid ← (i + j)/2
MergeSort(A, i, mid )
MergeSort(A, mid + 1, j )
Merge(A, i, mid, j )
8. Merge Algorithm
The basic merging algorithms takes
Two input arrays, A[] and B[],
An output array C[]
And three counters aptr, bptr and cptr. (initially set
to the beginning of their respective arrays)
The smaller of A[aptr] and B[bptr] is copied to the next
entry in C i.e. C[cptr].
The appropriate counters are then advanced.
When either of input list is exhausted, the remainder of
the other list is copied to C.
27. Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
Array B is now
finished, copy
remaining elements of
array A in array C
cptr
C[]
bptr
aptr
12 14 34 56 66 89
28. Merge Algorithm
56 66 10814
A[]
3412 89
B[]
If A[aptr] < B[bptr]
C[cptr++] = A[aptr++]
Else
C[cptr++] = B[bptr++]
Array B is now
finished, copy
remaining elements of
array A in array C
cptr
C[]
bptr
aptr
12 14 34 56 66 89 108
30. Problem with Merge Sort
Merging two sorted lists requires linear extra
memory.
Additional work spent copying to the
temporary array and back through out the
algorithm.
This problem slows down the sort considerably.
31. Quick Sort
Division of arrays in such a way that
The sorted sub arrays do not need to be later
merged.
This can be done by rearranging the elements in
A(1:n) such that
A(i) <= A(j) for all i between 1 and m and all j
between m+1 and n.
Thus the elements in A(1:m) and A(m+1: n)
may be independently sorted. No merge is
32. Quick Sort Algorithm
To sort an array S
1. If the number of elements in S is 0 or 1, then
return.
2. Pick any element V in S. This is called pivot.
3. Partition S-{V} (the remaining elements in S)
into two disjoint groups:
S1= {x ∈ S – {V} | x <= V},
S2= {x ∈ S – {V} | x >= V}
4. Return {quickSort(S1) followed by v followed
by quickSort(S ) }
33. Quick Sort Partitioning
Rearrange the array such that
Element V (pivot) is in its final position
None of elements in A[1] .. A[m] is > V
None of elements in A[m+1] .. A[n] is < V
pivot
elements lower
than pivot
elements higher
than pivot
← unsorted → ← unsorted →
34. Quick Sort Partitioning
Partition step/strategy is a design
decision.
Picking the Pivot
Best strategy would be to pick the median
of the array.
Median of three partitioning.
35. Quick Sort Partitioning
Picking the Pivot:
A = 8, 1, 4, 9, 6, 3, 5, 2, 7, 0
Pick three elements randomly and the
median of these three numbers is the
pivot.
36. Quick Sort Partitioning
1 4 39 6 58 2 7 0
Left = 8, Right = 0,
Center = (Left + Right)/2 = 4
• 4th
elements of the array started with 0 is the
pivot => 6
1 4 39 6 58 2 7 0
37. Quick Sort Partitioning
Replace the pivot with the last element
1 4 39 6 58 2 7 0
1 4 39 0 58 2 7 6
i j
Two indicies, i starts from the first element.
j starts from the second last element
Move all the smaller elements to the left and all
the larger elements to the right (small and large is
relative to the pivot).
43. Quick Sort Partitioning
i
1 4 35 0 92 8 7 6
j
A[ j ] < pivot.
i and j crosses each other therefore swap A[ i ]
with pivot.
All less then pivot
1 4 35 0 62 8 7 9
All greater then
pivot
44. Quick Sort Partitioning
All less then pivot
Apply same strategy on
this sublist separately
1 4 35 0 62 8 7 9
All greater then
pivot
Apply same
strategy on this
sublist
separately
45. Quick Sort Algorithm
QuickSort(A, left, right) {
if right > left then {
Pivot = Partition(A, left, right);
QuickSort(A, left, pivot-1);
QuickSort(A, pivot+1, right);
}
}
46. Partition Algorithm
Partition(a[ ], left, right ) {
int i, j;
int Pivot = (left+right) / 2;
Swap(A[Pivot], A[right]);
i = left; j = right - 1;
Pivot = right;
while ( i < j ) {
while( a[ i ] <= a[Pivot]) i++;
while( a[ j ] >= a[Pivot]) j- -;
if ( i < j ) SWAP(a[i], a[ j ]);
}
Swap(A[ i ], A[Pivot])
return i
}
47.
48. Selection Sort
Algorithm
To sort an array A[1…n], consisting of n elements,
the selection sort procedure is as follows. Scan
array A[1..n] to find the minimum element.
Swap minimum element with A[1]
Scan sub-array A[2..n] to find the minimum element.
Swap minimum element with A[2]
Scan sub-array A[3..n] to find the minimum element.
Swap minimum element with A[3]
Scan sub-array A[n-1.n] to find the minimum
element. Swap minimum element with A[n-1]
At the end array A[1…n] is sorted