MERGE and Quick Sort
MERGE and Quick Sort
Merge Sort
Merge sort with n input size of array
Divide:
Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
Conquer
Sort the subsequences recursively using merge sort
When the size of the sequences is 1 there is nothing more
to do
Combine
Merge the two sorted sub sequences
Merge Sort
The array is A[O .. n - 1]
Dividing into two halves
A[0 .. n/2 -1] and A[n/2 .. n -1]
Sorting each of them recursively
Merging the two smaller sorted arrays into a single sorted
one.
Merge Sort
ALGORITHM Merge-sort(A[0 .. n - 1])
//Sorts array A[O .. n - 1] by recursive merge-sort
//Input: An array A[O .. n - 1] of orderable elements
//Output: Array A[O .. n - 1] sorted in non-decreasing order
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]
Merge-sort(B[0 .. (n/2) - 1])
Merge-sort(C[0 .. (n/2) -1])
Merge(B, C, A)
Merge Sort
ALGORITHM Merge(B[0 .. p- 1], C[0 .. q -1], A[0 .. p + q -1])
//Merges two sorted arrays into one sorted array
//Input: Arrays B[O .. p -1] and C[O .. q -1] both sorted
//Output: Sorted array A[O .. p + q -1] of the elements of Band C
i ← 0; j ← 0; k ← 0
While i < p and j < q do
if B[i] ≤ S C[j]
A[k] ← B[i]; i ← i + 1
else A[k] ← C[j]; j ← j + 1
k← k+1
If i = p
copy C[j .. q -1] to A[k .. p + q -1]
else copy B[i .. p -1] to A[k .. p + q -1]
Merge Sort
83297154
8329 7154
7 1 5 4
8 3 2 9
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2389 1457
12345789
Merge Sort
A[i] A[j]
8 3 If (A[i] ≤ A[j])
{
temp[k] ← A[i]
While(i ≤ mid) 3 8 i←i+1
{ k←k+1
temp[k] ← A[i] }
i←i+1 Else
k←k+1 {
} temp[k] ← A[j]
While(j ≤ high) j←j+1
{ k←k+1
temp[k] ← A[j]
j←j+1
k←k+1
}
Merge Sort
Algorithm Analysis
Merge sort with n input size of array
Basic Operation:
Comparison
Two recursive calls are made
Combine two sub-list
Merge Sort
Algorithm Analysis
T(n) = T(n/2) + T(n/2) + cn
Time taken by left sub Time taken by right Time taken for combining
list to get sorted sub list to get sorted two sub lists
Master Method
T(n) = T(n/2) + T(n/2) + cn
T(n) = 2T(n/2) + cn -------------------------------------------------------(1)
T(1) = 0 ---------------------------------------------------------------------(2)
Example:
Sort the following Random numbers
50, 30, 10, 90, 80, 20, 40, 70
Let consider
a array;
p is a Pivot value
p=a[low];
i=low+1;
j=high;
Low High
50 30 10 90 80 20 40 70
i j
Pivot
Dr.Tamilarasan.S (18CSL47) 4/2/2025 59
QUICKSORT
Step: 2
If a[i] ≤ Pivot, i will increment
30 ≤ 50, then increment i
Low High
50 30 10 90 80 20 40 70
Pivot i j
Step:3
i j
If a[i] ≤ Pivot, i will increment
10 ≤ 50, then increment i
Step: 4
Low High
50 30 10 90 80 20 40 70
Pivot i j
i j
If a[j] > Pivot, j will decrement
70 > 50, then decrement j
Step: 6
Low High
50 30 10 90 80 20 40 70
Pivot i j
If a[j] > Pivot, j will decrement
40 > 50, then j decrement will stop
Swap a[i] and a[j] (swap 90 and 40)
Step:7
Low High
50 30 10 40 80 20 90 70
i j
If a[i] < a[low] and a[j] > a[low] then start continue incrementing i and
decrementing j, until the false conditions are obtained 40 < 50 increment i
and 90 > 50 decrement j
Step: 8
Low High
50 30 10 40 80 20 90 70
Pivot i j
If a[i] ≤ Pivot, i will increment
80 > 50, then i increment will stop then a[j] > pivot decrement j, 20 > 50 so
stop decrement j
Swap a[i] and a[j] (swap 80 and 20)
Step:9
Low High
50 30 10 40 20 80 90 70
i j
a[i] < Pivot (20 < 50) and a[j] > Pivot (80 > 50) continue increment i and
decrement j.
Pivot is
Left sub list Shifted at its Right sub list
position
Dr.Tamilarasan.S (18CSL47) 4/2/2025 64
QUICKSORT
Step: 12
Consider left sub list
Low High
20 30 10 40
Pivot i j
Step: 13
Consider left sub list
Low High
20 30 10 40
Pivot i j
Low High
20 10 30 40
Pivot i j
Step: 14
Consider left sub list
Low High
20 10 30 40
Pivot i j
Low High
20 10 30 40
Pivot i, j
a[j] > Pivot, 30 > 20 then decrement j
Step: 15
Consider left sub list
Low High
20 10 30 40
Pivot i, j
Low High
20 10 30 40
Pivot j i
a[j] > Pivot, 10 > 20 then stop decrement j, here j crossed i , swap
a[j] and Pivot (swap 10 and 20)
Dr.Tamilarasan.S (18CSL47) 4/2/2025 68
QUICKSORT
Step: 17
Sorted left sub list
Low High
10 20 30 40
Step: 18
Apply right sub tree
Low High
80 70 90
Pivot i, j
Here, a[j] > Pivot (90 > 80) so decrement j
Low High
80 70 90
Pivot j i
Step: 19
Combine
10 20 30 40 50 70 80 90
Quick Sort
It is based on the divide-and conquer approach.
Quick Sort
It is a process of partition of problem.
Quick Sort
Algorithm
ALGORITHM Quicksort(A[l … r])
//Sorts a sub-array by quicksort
//Input: A sub-array A[L … r] of A[0 … n -1], defined by its left
//and right indices l and r
//Output: Sub-array A[l .. r] sorted in nondecreasing order
if (l < r)
s ←Partition(A[l .. r]) // s is a split position
Quicksort(A[l .. s- 1])
Quicksort(A[s + l…r])
Quick Sort
ALGORITHM Partition(A[l ... r])
//Partitions a sub array by Hoare’s algorithm, using the first element as a pivot
//Input: Sub array of array A[0..n − 1], defined by its left and right indices l and r (l<r)
//Output: Partition of A[l…r], with the split position returned as this function’s value
p := A[l]
i ←l+1; j ←r;
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( p, A[j ])
return j
Quick Sort
Three situations
Case - 1
(i < j)
exchange A[i] and A[j] and resume the scans by incrementing
i and decrementing j, respectively
i j
Quick Sort
Three situations
Case - 2
(i > j) then
exchange A[low] and A[j] and resume the scans by
incrementing i and decrementing j, respectively
j i
Quick Sort
Three situations
Case - 3
(i = j) then
The array partitioned, with the split positions = i = j:
i= j
P All are ≤ P =P All are ≥P
C(1) = 0
C(1) = 0