Sorting Algorithms: Sourabh Kumar Choudhary
Sorting Algorithms: Sourabh Kumar Choudhary
89 45 68 90 29 34 17
45 89 68 90 29 34 17
45 68 89 90 29 34 17
45 68 89 90 29 34 17
29 45 68 89 90 34 17
29 34 45 68 89 90 17
17 29 34 45 68 89 90
Insertion Sort Algo
Merge Sort
Before that we should know how to merge 2 sorted
array.
Suppose, we’ve 2 sorted arrays
A[3] = {2,5,8}
B[3] = {3,4,7}
SortedArray[6];
We’ll’ve 2 indices to each array.
Compare the elements of the array at the index positions.
Smaller should be moved to the sorted array.
Increment the corresponding index in the array from which
element was taken.
MergeSort Contd…
So, we know how to merge two sorted arrays.
Only we need is two sorted arrays.
But we’ve only one array which is unsorted.
Hmmm, This array is unsorted, but if an array has only
one element, then that array is always sorted.
So, can we divide our original array into sub arrays of
single elements & then start merging them.
Merge SortThisNow
Contd… start
is the merging
original these
array. arrays
Just break& it into
then merging the
smaller merged
arrays arrays
of size 1. till
you get the final array
8 3 82 3 2 9 9 7 7 1 5 1 4 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Merge Sort contd…
So, our sort algorithm should have 2 parts.
Divide the array into sub arrays, &
Merge them
MergeSort(A[0 .. n-1])
If (n > 1)
Copy A[0 .. n/2 – 1] to B[0 .. n/2 – 1]
Copy A[n/2 .. n – 1] to C[0 .. n/2 – 1]
MergeSort(B[0 .. n/2 – 1])
MergeSort(C[0 .. n/2 – 1])
Merge(B,C,A)
Merger likhne ki jaroorat nai hai na… Ho jaayega na???
Quick Sort
We’ve used Divide & Conquer to solve our problem in Merge Sort.
Merge Sort – Division is based on position.
Quick Sort – Division is based on the elements value.
Rearrange elements of A[0 .. N-1] to achieve a partition
A[N] = A[0], A[1], .. ,A[s-1], A[s], A[s+1], .. , A[N-1]
Partition is about A[s], such that,
To it’s left all are smaller & to it’s right all are greater than A[s],
But left & right sub array need not be sorted
But A[s] is on it’s final position in the sorted array
3 5 2 7 11 9 10 8
Now, same partition can be formed in the two sub arrays.
Quick Sort contd…
QuicSort(A[L .. R])
If (L < R)
S = Partition(A[L .. R]) // A[S] is the split position, it’s final
QuickSort(A[L .. S-1])
QuickSort(A[S+1 .. R])
To start from take A[0] = 5 as pivot. Original Array –
3, 1, 2,4 are all less than 5
So, we’ll partition the array aroundHow 5this partition
L = 0 is done?
Quick Sort Contd… So, all of them should be on left of 5
And
We’ll see
the
We are left with 2 sub
R =later…
rest
7
on right
2 5is the pivot in this arrays And
Condition of3 our 1
Algo L < R 9 8 2 4 8, 79, 7
sub array 2, 3, 1, 4
is false here.
Partition will yield the
No
2 need 3to continue.1 same4array 5 8 9 7
1 2 3 4 5 8 9 7
Pivot
L<R=
1 2 3 4 5 L8< R = FALSE
9 7 FALSE
7 8 9
1 2 3 4 5 7 8 9
Quick Sort - Partition
Let’s have the same array. It can be
any sub array 9 which
< 5 = FALSE
we have to
5 cannot be
partition. A[Lon.. right
R] of 9
3 < 5 (Pivot)
1 < 5 (Pivot) 4 > 5 (Pivot)
7 > 5= (Pivot)
F
So, startLscanning
Initially 5 >R8==N-1
= 0, from
False
2 > the
5 = Move Next
False
Move Next
Move--->
NextMove Next Don’t Move Next Next
Pivot END,--->
for the elements
Stop biggerSTOP From < ---Move
Here
5 should be on right
5 should From
be on rightHere 5 should5be on right
should be on left
than 5
25 3 1 9
4 8
25 8
2 9
4 7