Lecture Week 3 2quick Sort - Merge Sort 26022024 104041am
Lecture Week 3 2quick Sort - Merge Sort 26022024 104041am
7 2 1 6 8 5 3 4
Pivot
2 1 3 4 8 5 7 6
Partition(A,start,end)
{ i Pindex Start end
Pivot=A[end] 7 3 0 7
Pindex=start
for(i=start to end-1)
{ QuickSort(A[ ] ,start , end)
If(A[i]<=pivot) {
{ if(start<end)
swap(A[i],A[pindex]) {
Pindex=pindex+1} Pindex=Partition(A , start,end)
} QuickSort(A,start,pindex-1)
Swap(A[Pindex],A[end] QuickSort(A,pindex+1,end)
return pindex; }
} }
2 1 3 4 8 5 7 6
2 1 3 8 5 7 6
2 1 3 4 8 5 7 6
Partition(A,start,end)
{ i Pindex Start end
Pivot=A[end] 1 2 0 2
Pindex=start
for(i=start to end-1)
{ QuickSort(A[ ] ,start , end)
If(A[i]<=pivot) {
{ if(start<end)
swap(A[i],A[pindex]) {
Pindex=pindex+1} Pindex=Partition(A , start,end)
}
Swap(A[Pindex],A[end] QuickSort(A,start,pindex-1)
return pindex;
} QuickSort(A,pindex+1,end)
}
}
2 1 3 4 8 5 7 6
2 1 3 8 5 7 6
1 2
2 1 3 4 8 5 7 6
2 1 3 5 6 7 8
1 2
1
2 1 3 4 8 5 7 6
2 1 3 5 6 7 8
1 2 5 7 8
1 2 3 4 5 6 7 8
10 80 30 90 40 50 70
10 30 80 90 40 50 70
Pivot=70
Partition(A,start,end) 10 30 80 90 40 50 70 i pind
{ ex
Pivot=A[end]
Pindex=start If(A[i]<=pivot) 0 0
for(i=start to end-1) (A[4]<=70)
40<=70
1 1
{
If(A[i]<=pivot) Swap(A[i],A[pindex]) 2 1
SwapA[4],A[2]
{ 3 2
swap(A[i],A[pindex]) 10 30 40 90 80 50 70
Pindex=pindex+1} 4 2
} 5 3
Swap(A[Pindex],A[end] If(A[i]<=pivot)
} (A[5]<=70) 6 4
50<=70
Swap(A[i],A[pindex])
SwapA[5],A[3]
10 30 40 50 80 90 70
Partition(A,start,end) i pind
{ Pivot=70,end=7 ex
Pivot=A[end]
Pindex=start 10 30 40 50 80 90 70
for(i=start to end-1)
{
If(A[i]<=pivot)
{ Swap(A[pindex],A[end]
swap(A[i],A[pindex]) Swap(A[4],A[6]
Pindex=pindex+1}
}
Swap(A[Pindex],A[end] 10 30 40 50 70 90 80
}
10 30 40 50 70 90 80
Partition(A,start,end)
{
Pivot=A[end]
Pindex=start
for(i=start to end-1)
{
If(A[i]<=pivot)
{
swap(A[i],A[pindex])
Pindex=pindex+1}
}
Swap(A[Pindex],A[end]
}
10 30 40 50 70 90 80
Partition(A,start,end)
{
Pivot=A[end]
Pindex=start
for(i=start to end-1)
{
If(A[i]<=pivot)
{
swap(A[i],A[pindex])
Pindex=pindex+1}
}
Swap(A[Pindex],A[end]
}
In the end you will get sorted array
10 30 40 50 70 80 90
Time complexity
▪ Runtime: O(N log N). Somewhat faster for ascending/ descending input.
Merge sort-Example 1
38 27 43 3 9 82 10
38 27 43 3 9 82 10
38 27 43 3 9 82 10
38 27 43 3 9 82 10
38 27 43 3 9 82 10
27 38 3 43 9 82 10
3 27 38 43 9 10 82
Left Array
exhausted
3 9 10 27 38 43 82
Merge sort-Example 1
After dividing the array into smallest units, now merging starts,
based on comparison of elements.
27 38 3 43 9 82 10
3 27 38 43 9 10 82
Left Array
exhausted
3 9 10 27 38 43 82
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat smallest
until done. smallest
A G L O R H I M S T
A G H I L auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat untilsmallest
done. smallest
A G L O R H I M S T
A G H I L M auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat untilsmallest
done. smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Example 2
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
first half
Repeat until done.
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Example 2
Mergesort (divide-and-conquer)
Divide array into two halves.
A L G O R I T H M S
A L G O R I T H M S divide
Example 2
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Example 2
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Example 3
index 0 1 2 3 4 5 6 7
value 22 18 12 -4 58 7 31 42
spli
t
22 18 12 -4 58 7 31 42
spli spli
t t
22 12 - 58 7 31
spli 18 spli 4 spli spli 42
t 22 18 t 12 -4 t 58 7 t 31 42
merg merg merg merg
e 18 e -4 e 7 e 31
22
merg 12 58
merg 42
e -4 12 18 22 e 7 31 42 58
merg
e -4 7 12 18 22 31 42 58
40
Algorithm
12 11 13 5 6 7
12 11 13 5 6 7
12 11 13
12 11
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
void merge(int arr[], int l, int m, int r) arr[k] = L[i];
{ i++;
int n1 = m - l + 1; }
int n2 = r - m; else {
arr[k] = R[j];
// Create temp arrays j++;
int L[n1], R[n2]; }
k++;
// Copy data to temp arrays L[] and R[] }
for (int i = 0; i < n1; i++)
L[i] = arr[l + i]; // Copy the remaining elements of
for (int j = 0; j < n2; j++) // L[], if there are any
R[j] = arr[m + 1 + j]; while (i < n1) {
arr[k] = L[i];
// Merge the temp arrays back into arr[l..r] i++;
k++;
// Initial index of first subarray }
int i = 0;
// Copy the remaining elements of
// Initial index of second subarray // R[], if there are any
int j = 0; while (j < n2) {
arr[k] = R[j];
// Initial index of merged subarray j++;
int k = l; k++;
}
}
Time Complexity-Comparison
Helpful links
https://www.youtube.com/watch?v=TzeBrDU-
JaY