0% found this document useful (0 votes)
15 views40 pages

MERGE and Quick Sort

The document provides an overview of the Merge Sort and Quick Sort algorithms, detailing their divide-and-conquer approaches. Merge Sort involves dividing an array into two halves, sorting them recursively, and then merging the sorted halves, while Quick Sort partitions the array around a pivot element and recursively sorts the sub-arrays. Both algorithms are analyzed for their efficiency and operational steps are illustrated with examples.

Uploaded by

ananya.r.amcec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views40 pages

MERGE and Quick Sort

The document provides an overview of the Merge Sort and Quick Sort algorithms, detailing their divide-and-conquer approaches. Merge Sort involves dividing an array into two halves, sorting them recursively, and then merging the sorted halves, while Quick Sort partitions the array around a pivot element and recursively sorts the sub-arrays. Both algorithms are analyzed for their efficiency and operational steps are illustrated with examples.

Uploaded by

ananya.r.amcec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Merge 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

Dr.Tamilarasan.S (18CS42) 4/2/2025 49


Merge Sort

 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.

Dr.Tamilarasan.S (18CS42) 4/2/2025 50


Merge Sort

 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)

Dr.Tamilarasan.S (18CS42) 4/2/2025 51


Merge Sort

 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]

Dr.Tamilarasan.S (18CS42) 4/2/2025 52


Merge Sort

 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

Dr.Tamilarasan.S (18CS42) 4/2/2025 53


Merge Sort

 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
}

Dr.Tamilarasan.S (18CS42) 4/2/2025 54


Merge Sort

 Merge Sort
 Algorithm Analysis
 Merge sort with n input size of array
 Basic Operation:
 Comparison
 Two recursive calls are made
 Combine two sub-list

Dr.Tamilarasan.S (18CS42) 4/2/2025 55


Merge Sort

 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

Where n > 1 and T(1) = 0

 Master Method
T(n) = T(n/2) + T(n/2) + cn
T(n) = 2T(n/2) + cn -------------------------------------------------------(1)
T(1) = 0 ---------------------------------------------------------------------(2)

Dr.Tamilarasan.S (18CS42) 4/2/2025 56


Merge Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 57


QUICKSORT

 Follows the divide-and-conquer paradigm.


 Divide: Partition (separate) the array into two (possibly empty) sub-arrays
 Conquer: Sort the two sub-arrays by recursive calls to quicksort
 Combine: The sub-arrays are sorted in place – no work is needed to
combine them.

A[0] …………. A[m - 1] A[m] A[m + 1] …….. A[n – 1]

Mid value These elements are greater than A[m]


These elements are less than A[m]

Dr.Tamilarasan.S (18CSL47) 4/2/2025 58


QUICKSORT

 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

Dr.Tamilarasan.S (18CSL47) 4/2/2025 60


QUICKSORT

 Step: 4
Low High
50 30 10 90 80 20 40 70
Pivot i j

 If a[i] ≤ Pivot, i will increment


 90 ≤ 50, then increment i will stop
 Step:5

i j
 If a[j] > Pivot, j will decrement
 70 > 50, then decrement j

Dr.Tamilarasan.S (18CSL47) 4/2/2025 61


LAB-4 QUICKSORT

 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

Dr.Tamilarasan.S (18CSL47) 4/2/2025 62


QUICKSORT

 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.

Dr.Tamilarasan.S (18CSL47) 4/2/2025 63


QUICKSORT
 Step: 10
Low High
50 30 10 40 20 80 90 70
Pivot i, j
Low High
50 30 10 40 20 80 90 70
j i
 If a[i] < a[low] and j has crossed i. that is j < i, then swap a[low] or Pivot
and a[j]. ( swap 50 and 20).
 Step: 11
Low High
20 30 10 40 50 80 90 70

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

 If a[i] ≤ Pivot then start increment I


 30 ≤ 20 hen stop increment I
 Then if a[j] > Pivot then decrement j
 40 > 20 then start decrement j

Dr.Tamilarasan.S (18CSL47) 4/2/2025 65


QUICKSORT

 Step: 13
 Consider left sub list
Low High
20 30 10 40
Pivot i j

 Then if a[j] > Pivot then decrement j


 10 > 20 then stop decrement j
 Swap a[i] and a[j]

Low High
20 10 30 40
Pivot i j

Dr.Tamilarasan.S (18CSL47) 4/2/2025 66


QUICKSORT

 Step: 14
 Consider left sub list
Low High
20 10 30 40
Pivot i j

 Then if a[i] ≤ Pivot then increment i


 10 ≤ 20 then increment I

Low High
20 10 30 40
Pivot i, j
 a[j] > Pivot, 30 > 20 then decrement j

Dr.Tamilarasan.S (18CSL47) 4/2/2025 67


QUICKSORT

 Step: 15
 Consider left sub list

Low High
20 10 30 40
Pivot i, j

 a[j] > Pivot, 30 > 20 then decrement 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

Apply right sub tree


Low High
80 70 90
Pivot i j

 Here a[i] < Pivot (70 < 80) then increment i

Dr.Tamilarasan.S (18CSL47) 4/2/2025 69


QUICKSORT

 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

Now swap Pivot and a[j] (Swap 80 and 90)


Low High
70 80 90
Dr.Tamilarasan.S (18CSL47) 4/2/2025 70
QUICKSORT

 Step: 19
 Combine

10 20 30 40 50 70 80 90

Dr.Tamilarasan.S (18CSL47) 4/2/2025 71


QUICKSORT

Quick Sort
 It is based on the divide-and conquer approach.

 it rearranges elements of a given array A(0 .. n - 1] with respect to


partition (s).

 All the elements before positions are less than A[s]

 All the elements after positions are greater than A[s]

 A[0] ... A[s -1] A[s] A[s + 1] ... A[n -1]

all are < A(s] all are > A(s]

Dr.Tamilarasan.S (18CSL47) 4/2/2025 72


QUICKSORT

Quick Sort
 It is a process of partition of problem.

 To achieve Partition, we need to choose an element from the


array called Pivot.

 The first element in the array is pivot point

 Partition can be done by double Scan approach

Dr.Tamilarasan.S (18CSL47) 4/2/2025 73


QUICKSORT

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])

Dr.Tamilarasan.S (18CSL47) 4/2/2025 74


QUICKSORT

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

Dr.Tamilarasan.S (18CSL47) 4/2/2025 75


QUICKSORT

Quick Sort
 Three situations

 Case - 1

 If scanning indices i and j have not crossed

 (i < j)
 exchange A[i] and A[j] and resume the scans by incrementing
i and decrementing j, respectively

i j

P All are ≤ P ≥P ………… ≤P All are ≥P

Dr.Tamilarasan.S (18CSL47) 4/2/2025 76


QUICKSORT

Quick Sort
 Three situations

 Case - 2

 If scanning indices i and j have crossed

 (i > j) then
 exchange A[low] and A[j] and resume the scans by
incrementing i and decrementing j, respectively

j i

P All are ≤ P ≥P ≤P All are ≥P

Dr.Tamilarasan.S (18CSL47) 4/2/2025 77


QUICKSORT

Quick Sort
 Three situations

 Case - 3

 If scanning indices i and j stop while pointing to the same


element

 (i = j) then
 The array partitioned, with the split positions = i = j:

i= j
P All are ≤ P =P All are ≥P

Dr.Tamilarasan.S (18CSL47) 4/2/2025 78


QUICKSORT

Quick Sort-Algorithm Analysis


Best case (Split in the Middle)

Recurrence relation for quick sort

C(n) = C(n / 2) + C(n / 2) + n

Time required to Time required to Time required for


sort left sub array sort right sub array partitioning the
sub array

C(1) = 0

Dr.Tamilarasan.S (18CSL47) 4/2/2025 79


QUICKSORT

Quick Sort-Algorithm Analysis


Best case (Split in the Middle)
Recurrence relation for quick sort
C(n) = C(n / 2) + C(n / 2) + n
C(n) = 2C(n / 2) + n ---------------------(1)
C(1) = 0 --------------------------------------------(2)

Dr.Tamilarasan.S (18CSL47) 4/2/2025 80


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 81


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 82


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 83


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 84


Quick Sort

C(1) = 0

Dr.Tamilarasan.S (18CS42) 4/2/2025 85


Quick Sort

 Quick Sort - Worst Case Time Complexity

Dr.Tamilarasan.S (18CS42) 4/2/2025 86


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 87


Quick Sort

Dr.Tamilarasan.S (18CS42) 4/2/2025 88

You might also like