0% found this document useful (0 votes)
17 views

Lecture Week 3 2quick Sort - Merge Sort 26022024 104041am

Quicksort and mergesort are divide-and-conquer sorting algorithms. Quicksort works by recursively selecting a pivot element and partitioning the array around it, placing elements less than the pivot to the left and greater elements to the right. Mergesort works by recursively dividing the array in half and sorting each half, then merging the sorted halves back together. Both algorithms have average-case time complexity of O(n log n).

Uploaded by

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

Lecture Week 3 2quick Sort - Merge Sort 26022024 104041am

Quicksort and mergesort are divide-and-conquer sorting algorithms. Quicksort works by recursively selecting a pivot element and partitioning the array around it, placing elements less than the pivot to the left and greater elements to the right. Mergesort works by recursively dividing the array in half and sorting each half, then merging the sorted halves back together. Both algorithms have average-case time complexity of O(n log n).

Uploaded by

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

Quick Sort & Merge Sort

Lecture week 3_2


Divide-and-Conquer

 Divide-and-Conquer (Input: Problem P)


 To Solve P:
1. Divide P into smaller problems P1, P2,
P3.....Pk.
2. Conquer by solving the (smaller) sub
problems recursively.
3. Combine solutions to P1, P2, ...Pk into
solution for P.
Quick Sort
 Quicksort is a divide-and-conquer sorting algorithm
in which division is dynamically carried out.
 Divide: Rearrange the elements and split the array
into two sub arrays and an element in between such
that so that each element in the left subarray is less
than or equal the middle element and each element
in the right subarray is greater than the middle
element.

 Conquer: Recursively sort the two subarrays


Quick Sort

QuickSort(A[ ] ,start , end)


{
if(start<end)
{
Pindex=Partition(A , start,end)
QuickSort(A,start,pindex-1)
QuickSort(A,pindex+1,end)
}
}
Partitioning Array
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements


below/above pivot.
Quick sort
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]
return pindex;
}
Select pivot

There are a number of ways to pick the pivot element. In this


example, we will use the last element in the array

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

Partition(A,start,end) Pivot=70 pindex=0 i pindex


{ If(A[i]<=pivot)
A[0]<=70 0 0
Pivot=A[end]
Swap(A[i],A[pindex]
Pindex=start 1 1
SwapA[0],A[0]
for(i=start to end-1)
1 8 3 9 4 5 70
2 1
{
If(A[i]<=pivot) 0 0 0 0 0 0
{ If(A[1]<=70)
swap(A[i],A[pindex]) 80<=70no
Pindex=pindex+1}
}
Swap(A[Pindex],A[end] 10 80 30 90 40 50 70
}
Pivot=70
Partition(A,start,end) 10 80 30 90 40 50 70 i pindex
{
Pivot=A[end] 0 0
Pindex=start If(A[i]<=pivot) 1 1
for(i=start to end-1) (A[2]<=70)
{ 30<=70 2 1
If(A[i]<=pivot) Swap(A[i],A[pindex])
SwapA[1],A[2] 3 2
{
swap(A[i],A[pindex]) 4 2
10 30 80 90 40 50 70
Pindex=pindex+1}
}
Swap(A[Pindex],A[end] If(A[i]<=pivot)
} (A[3]<=70)
90<=70no

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

Elements less than Pivot


Elements greater than
Pivot Pivot
10 30 40 50 70 90 80

Consider this half


Pivot =50
Run the algorithm again

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

Consider this half


Pivot =80
Run the algorithm again

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

 Best case = O(nlogn)


 Average case = O(nlogn)
 Worst case = O(
Merge Sort
Merge Sort
 Repeatedly divides the data in half, sorts each half, and combines the
sorted halves into a sorted whole.
 The algorithm:
▪ Divide the list into two roughly equal halves.
▪ Sort the left half.
▪ Sort the right half.
▪ Merge the two sorted halves into one sorted list.

▪ Often implemented recursively.


▪ An example of a "divide and conquer" algorithm.
• Invented by John von Neumann in 1945

▪ 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

void mergeSort(int arr[],int l,int r){


if(l>=r){
return;//returns recursively
}
int m =l+ (r-l)/2; M= 0+ 5 /2
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
int arr[] = { 12, 11, 13, 5, 6, 7 };
size=6
Mergesort(arr, 0,size-1);

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

You might also like