You are on page 1of 14

Sorting Algorithms

Sourabh Kumar Choudhary


Contents
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Merge Sort
Heap Sort
Insertion Sort
In each iteration, when I’m on Array[i], assuming
Array is sorted upto Array[i-1], I’ll search appropriate
position of Array[i] in the sorted array & insert it there.
This may include shifting of array elements.
So, we’ll start from the second element of the array.
Next Element = 68.
Start from 45, be after 45 & Original
Insertion Sort Contd…
It should
Should be b4 89b4 89 Array

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

Now, Our Forward & Backward Scan has overlapped.


Stop further
We’ve not scanned the fullscan.
array, & stopped
had to stop.
5 should be on Right of 2 & Left of 8. We
Which is on 8 & 2.
possible.
5 cannot go to Right of 9 & 5 Butshould5 be on be
should Right
on of 4 of 2 &
Right
How???
So, what to do NOW???
Swap the last left scanned element Itwith
cannot
the be on right of 8
Pivot???
Swap 9 & 4 – Then we can proceed.
Why??? Start
– Think from So, Swap 8 & 2
ScanYourself…
This we’ll continue to do, till we scan the full array, i.e., the scan overlaps
I= L + So,
SWAP 1,(5Scan
, 2) should
Forward & continue untill we overlap
J = R, Backwards
QuickSort – Partition Algo
Partition(A[L .. R])
 P = A[L]
 I = L, J = R + 1
 Repeat
Repeat I = I + 1 until A[i] >= P
Repeat J = J - 1 until A[j] <= P
Swap(A[i], A[j])
 Until (I >= J)
 Swap ( A[i], A[j]) // Undo last swap when I >= J
 Swap (A[L], A[J]) // Swap with the PIVOT
 Return J

You might also like