Sorting
Sorting
Sorting
1
Sorting Algorithms
2
Bubble Sort
Bubble Sort
Successively bubbles up largest elements
one by one, to the end of the array
Selection Sort
Selects minimum element from the
remaining array and places it in the
beginning of the array
Insertion Sort
Inserts every element at its right place in a
sorted array.
Merge Sort
T(N) = 2.T(N/2) + cN
T(N) = 2.[2T(N/4)+cN/2] + cN
= 4.T(N/4) + 2.cN/2
+ cN
= 4.T(N/4) + 2.cN
= 22.T(N/22) + 2.cN
Now, 4.T(N/4) + 2.cN can be
further solved to obtain
= 8.T(N/8) + 3.cN
= 23.T(N/23) + 3.cN
Say T(N) = 2k.T(N/2k) + k.cN
(if mergeSort goes for k
recursions)
Merge Sort
0 1 2 3 4 5 6 7 8
-1 4 7 2 1 5 3 0 9
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
-1 4 7 2 1
Merge Sort
5 6 7 8
2 3 4
0 1
5 3 0 9
7 2 1
-1 4
Merge Sort
5 6 7 8
2 3 4
0
1 5 3 0 9
7 2 1
4
-1
Merge Sort
5 6 7 8
2 3 4
0 1
5 3 0 9
7 2 1
-1 4
Merge Sort
5 6 7 8
3 4
0 1 2
5 3 0 9
2 1
-1 4 7
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
2 1
-1 4 7
Merge Sort
5 6 7 8
0 1 2 4
3
5 3 0 9
1
-1 4 7 2
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
-1 4 7 2 1
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
-1 4 7 1 2
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
-1 1 2 4 7
Merge Sort
5 6 7 8
0 1 2 3 4
5 3 0 9
-1 1 2 4 7
0 1 2 3 4 5 6 7 8
-1 1 2 4 7 0 3 5 9
Merge Sort
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 2 4 7 0 1 3 5 9
i j
-1 0 1 2 3 4 5 7 9
k
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j
IN-PLACE = Without using a third
array (and only working on indices at
different positions in the array)
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j
-1 < 1 ⇒ i++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j
Result array
0 < 1 ⇒ i++
Note how first sub-array shrinks when something from first sub-array
contributes to the building of resultant array
WHATEVER is BEHIND (to the left of) “i” becomes the result array
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j
0 1 2 3 4 5 6 7 8
-1 0 2 4 7 1 3 5 9
i mid j 2>1
- Temp = 1
0 1 2 3 4 5 6 7 8
-1 0 2 2 4 7 3 5 9
i mid j
2>1
- Temp = 1
- Shift 7, 4, 2 to the
right
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
2>1
- Temp = 1
- Shift 7, 4, 2 to the
right
- Arr[i] = temp
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
2>1
- I++
- Mid++ (something from second half was added to
result. . so an element of second half is deleted and
moved and the boundary of first half is shifted right)
- J++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
Note how second sub-array shrinks and first sub-array moves to the
right when something from second sub array contributes towards
building the resulting sub-array
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
2 < 3 ⇒ i++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 7 3 5 9
i mid j
4>3
- Temp = 3
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 4 4 7 5 9
i mid j
4>3
- Temp = 3
- Shift 7, 4 to the right
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
i mid j
4>3
- Temp = 3
- Shift 7, 4 to the right
- Arr[i] = temp
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
i mid j
4>3
- i++
- mid++
- j++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
i mid j
4>3
- i++
- mid++
- j++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
i mid j
4<5
- i++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
mid j
i
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 5 9
mid j
7>5
- Temp = 5
i
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 7 7 9
mid j
7>5
- Temp = 5
- Shift 7 to right i
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
mid j
7>5
- Temp = 5
- Shift 7 to right i
- Arr[i] = temp
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
mid j
7>5
- I++
- Mid++ i
- J++
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
mid j
i<=mid
i
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
mid j
7 < 9 ⇒ i++
and
i becomes greater i
than mid
Merging the arrays (in sorted orders)
0 1 2 3 4 5 6 7 8
-1 0 1 2 3 4 5 7 9
Array Sorted !
Quick Sort
● Quick Sort
○ Select a pivot
■ First element
■ Last element
■ Any random element
■ Select middle element as the pivot
○ Place pivot at its proper place in the array
■ Elements less than pivot are on one side
■ Elements greater than pivot are on the other side
of pivot
○ Perform quickSort on the two halves
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 7 2 12 15 13 0 6
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 7 2 12 15 13 0 6
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 7 2 12 15 13 0 6
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 7 2 12 15 13 0 6
pointer pivot
7 is greater than 6, swap the
elements as well as the pivot and
pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 6 2 12 15 13 0 7
pivot pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 6 2 12 15 13 0 7
pivot pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 6 2 12 15 13 0 7
pivot pointer
0 is less than 6
=> Swap the elements as well as
the pivot and pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 12 15 13 6 7
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 12 15 13 6 7
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 12 15 13 6 7
pointer pivot
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
pivot pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
pivot pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
pivot pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
pivot
pointer
Quick Sort
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
0 1 2 3 4 5 6 7 8
-1 4 0 2 6 15 13 12 7
1 9 7 0 4 5 3 2 -1
In-place sort = sorting the arrray without using extra space (like an
extra array) . . .or sorting that uses O(1) space apart from O(n) which
is used store the array which is to be sort
Stable sort = If unsorted array has two equal entries A and B (such
that A occurs before B, then A will occur before B in the sorted array
also. . . such a sorting is called Stable Sort. . .
Heap Sort
1 9 7 0 4 5 3 2 -1
1 9 7 0 4 5 3 2 -1
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
N=9
Now start from parent of N/2-1 = 3
last node = (N/2-1) and Call maxHeapify() as
move up to the root follows
WHILE maxHeapifying
the tree at every node. . . maxHeapify(arr,N,3)
maxHeapify(arr,N,2)
maxHeapify(arr,N,1)
maxHeapify(arr,N,0)
Heap Sort
indexOfLargest
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
indexOfLargest rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
indexOfLargest rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
0 1 2 3 4 5 6 7 8
1 9 7 0 4 5 3 2 -1
0 1 2 3 4 5 6 7 8
1 9 7 2 4 5 3 0 -1
0 1 2 3 4 5 6 7 8
1 9 7 2 4 5 3 0 -1
We have sent zero down the heap. . which may violate heap property with its own
children. .
[Note: IndexOfLargest will be at a position from where the largest element was
picked for swapping,i.e, Left child or right child. If the swapping happened at all]
Therefore, call maxHeapify(arr, N=9, currentIndex = indexOfLargest)
Heap Sort
indexOfLargest
currentIndex rightChildIndex
leftChildIndex
maxHeapify(arr, N=9, currentIndex = 3)
0 1 2 3 4 5 6 7 8
1 9 7 2 4 5 3 0 -1
We have sent zero down the heap. . which may violate heap property with its own
children. .
Also note that if swapping happened, then indexOfLargest will not be at
currentIndex, it will be either at left child or at right child of the current index
(or indexOfLargest will be down the tree)
Heap Sort
9 4 7 2 1 5 3 0 -1
Heap Sort
0 1 2 3 4 5 6 7 8
9 4 7 2 1 5 3 0 -1
0 1 2 3 4 5 6 7 8
-1 4 7 2 1 5 3 0 9
0 1 2 3 4 5 6 7 8
-1 4 7 2 1 5 3 0 9
0 1 2 3 4 5 6 7 8
-1 4 7 2 1 5 3 0 9
0 1 2 3 4 5 6 7 8
7 4 -1 2 1 5 3 0 9
Now call maxHeapify on Index 2 (or the index with which the swap
happened)
Heap Sort
0 1 2 3 4 5 6 7 8
7 4 -1 2 1 5 3 0 9
0 1 2 3 4 5 6 7 8
7 4 -1 5 1 -1 3 0 9
0 1 2 3 4 5 6 7 8
7 4 -1 5 1 -1 3 0 9
0 1 2 3 4 5 6 7 8
7 4 -1 5 1 -1 3 0 9
0 1 2 3 4 5 6 7 8
7 4 -1 5 1 -1 3 0 9
After the remaining array has been max heapified. . .i value will again
decrease and the length of heap will decrease by 1)
Heap Sort
0 1 2 3 4 5 6 7 8
0 4 -1 5 1 -1 3 7 9