![Page 1: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/1.jpg)
Big-O and Sorting
February 6, 2006
![Page 2: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/2.jpg)
Administrative Stuff
• Readings for today: Ch 7.3-7.5
• Readings for tomorrow: Ch 8
![Page 3: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/3.jpg)
Sorting!
• Very common to need data in order– Viewing, printing– Faster to search, find min/max, compute
median/mode, etc.
• Lots of different sorting algoritms– From the simple to very complex– Some optimized for certain situations (lots of
duplicates, almost sorted, etc.)– Typically sort arrays, but algorithms usually can be
adapted for other data structures (e.g. linked lists)
![Page 4: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/4.jpg)
Selection sort
• Sort by "selecting" smallest and putting in front– Search entire array for minimum value– Min is placed in first slot– Could move elements over to make space, but
faster to just swap with current first– Repeat for second smallest, third, and so on
![Page 5: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/5.jpg)
Selection sort codevoid SelectionSort(int arr[], int n){ for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[minIndex]) minIndex = j; } Swap(arr[i], arr[minIndex]); }}
![Page 6: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/6.jpg)
Analyzing selection sort
for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[minIndex]) minIndex = j; } Swap(arr[i], arr[minIndex]); }
• Count statements– First time inner loop N-1 comparisons– N-2 second time, then N-3, …– Last iteration 1 comparison
![Page 7: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/7.jpg)
Analyzing selection sort
• N-1 + N-2 + N-3 + … + 3 + 2 + 1– "Gaussian sum"
• Add sum to self
Sum =
![Page 8: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/8.jpg)
Analyzing selection sort• N-1 + N-2 + N-3 + … + 3 + 2 + 1
– "Gaussian sum"
• Add sum to self
N-1 + N-2 + N-3 + … + 3 + 2 + 1
+ 1 + 2 + 3 + …. + N-2 + N-1
= N + N + N + …. + N + N
= (N-1)N
Sum = 1/2 * (N-1)N
O(N2)
![Page 9: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/9.jpg)
Quadratic growth• In clock time
– 10,000 3 sec– 20,000 13 sec– 50,000 77 sec– 100,000 5 min
• Double input -> 4X time– Feasible for small inputs, quickly unmanagable
• Halve input -> 1/4 time– Hmm…– If two sorted half-size arrays, how to produce sorted full
array?
![Page 10: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/10.jpg)
Mergesort• "Divide and conquer" algorithm
– Divide array in half– Recursively sort each half– Merge two halves together
• "Easy-split hard-join"– No complex decision about which goes where,
just divide in middle– Merge step preserves ordering from each half
![Page 11: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/11.jpg)
6 2 8 4 10 7 1 5 9 3
![Page 12: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/12.jpg)
void MergeSort(int array[], int n){ if (n > 1) { int n1 = n/2; int n2 = n - n1; int *arr1 = CopySubArray(array, 0, n1); int *arr2 = CopySubArray(array, n1, n2);
MergeSort(arr1, n1); MergeSort(arr2, n2);
Merge(array, arr1, n1, arr2, n2);
delete[] arr1; delete[] arr2; }}
![Page 13: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/13.jpg)
CopySubArray
// Create a new array in memoryvoid CopyArray(int arr[], int n, int * & copy){ copy = new int[n];
for(int i = 0; i < n; i++) { copy[i] = arr[i];}
}
![Page 14: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/14.jpg)
Merge codevoid Merge(int array[], int arr1[], int n1, int arr2[], int n2){ int p = 0, p1 = 0, p2 = 0;
while (p1 < n1 && p2 < n2) { if (arr1[p1] < arr2[p2]) array[p++] = arr1[p1++]; else array[p++] = arr2[p2++]; } while (p1 < n1) array[p++] = arr1[p1++]; while (p2 < n2) array[p++] = arr2[p2++];}
![Page 15: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/15.jpg)
void Merge(int array[], int arr1[], int n1, int arr2[], int n2){ int p, p1, p2; p = p1 = p2 = 0; while (p1 < n1 && p2 < n2) { // Merge until hit if (arr1[p1] < arr2[p2]) { // end of one array array[p++] = arr1[p1++]; } else { array[p++] = arr2[p2++]; } } while (p1 < n1) { // Merge rest of array[p++] = arr1[p1++]; // remaining array } while (p2 < n2) { array[p++] = arr2[p2++]; }}
4array
arr1
arr2
n1
n2
p1
p2
p
7 8 12 4
5 9 16 18 4
![Page 16: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/16.jpg)
Merge sort analysis
void MergeSort(int array[], int n){ if (n > 1) { int n1 = n/2; int n2 = n - n1; int *arr1 = CopySubArray(array, 0, n1); int *arr2 = CopySubArray(array, n1, n2); MergeSort(arr1, n1); MergeSort(arr2, n2); Merge(array, arr1, n1, arr2, n2); delete[] arr1; delete[] arr2; }}
![Page 17: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/17.jpg)
MS(N)
Merge sort analysis= N
= N/2 + N/2MS(N/2) MS(N/2)+
N/4 N/4 N/4 N/4 = 4*N/4+
N/8 N/8 N/8 N/8 N/8 N/8 N/8 N/8+
= 8*N/8
Each level contributes N
...
![Page 18: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/18.jpg)
MS(N)
Merge sort analysis
MS(N/2) MS(N/2)
N/4 N/4 N/4 N/4
N/8 N/8 N/8 N/8 N/8 N/8 N/8 N/8
N/2K = 1 N = 2K
lg N = K
lg N levels * N per level= O(NlgN)
K levels
…
N/2K
![Page 19: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/19.jpg)
In clock time
• Compare SelectionSort to MergeSort– 10,000 3 sec .05 sec– 20,000 13 sec .15 sec– 50,000 78 sec .38 sec– 100,000 5 min .81 sec– 200,000 20 min 1.7 sec– 1,000,000 8 hrs (est) 9 sec
• O(NlgN) is looking pretty good! But can we do even better?
![Page 20: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/20.jpg)
Can we do even better than MergeSort?
• O(N log N) is fastest sort in the general case– So, theoretically, answer is “no”
• But, we can come up with a different O(N log N) sort that is practically faster
• Want to avoid overhead of creating new arrays (as is done in MergeSort)– Bring on the QuickSort!
![Page 21: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/21.jpg)
Quicksort
5 3 7 4 8 6 2 1
![Page 22: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/22.jpg)
Recursive Insight
5 3 7 4 8 6 2 1
![Page 23: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/23.jpg)
Recursive Insight
5 3 7 4 8 6 2 1
select “pivot”
![Page 24: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/24.jpg)
Partition array so:
• everything smaller than pivot is on left
• everything greater than or equal to pivot is on right
• pivot is in-between
Recursive Insight
5 3 7 4 8 6 2 1
![Page 25: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/25.jpg)
Partition array so:
• everything smaller than pivot is on left
• everything greater than or equal to pivot is on right
• pivot is in-between
Recursive Insight
2 3 1 4 5 6 8 7
![Page 26: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/26.jpg)
Recursive Insight
2 3 1 4 5 6 8 7
Now recursive sort “red” sub-array
![Page 27: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/27.jpg)
Recursive Insight
1 2 3 4 5 6 8 7
Now recursive sort “red” sub-array
![Page 28: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/28.jpg)
Recursive Insight
1 2 3 4 5 6 8 7
Now recursive sort “red” sub-array
Then, recursive sort “blue” sub-array
![Page 29: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/29.jpg)
Recursive Insight
1 2 3 4 5 6 7 8
Now recursive sort “red” sub-array
Then, recursive sort “blue” sub-array
![Page 30: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/30.jpg)
Recursive Insight
1 2 3 4 5 6 7 8
Everything is sorted!
![Page 31: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/31.jpg)
void Quicksort(int arr[], int n)
{
if (n < 2) return;
int boundary = Partition(arr, n);
// Sort subarray up to pivot Quicksort(arr, boundary);
// Sort subarray after pivot to end Quicksort(arr + boundary + 1, n – boundary - 1);
}
“boundary” is the index of the pivot
This is equal to the number of elements before pivot
![Page 32: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/32.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
![Page 33: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/33.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
![Page 34: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/34.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 35: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/35.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 36: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/36.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 37: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/37.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 38: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/38.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 39: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/39.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 7 4 8 6 2 1
pivotlh rh
![Page 40: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/40.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 41: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/41.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 42: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/42.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 43: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/43.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 44: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/44.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 45: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/45.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 46: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/46.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 47: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/47.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 48: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/48.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 8 6 2 7
pivotlh rh
![Page 49: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/49.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 50: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/50.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 51: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/51.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 52: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/52.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 53: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/53.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 54: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/54.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 55: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/55.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 56: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/56.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 57: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/57.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 58: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/58.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
5 3 1 4 2 6 8 7
pivotlh rh
![Page 59: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/59.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
2 3 1 4 5 6 8 7
pivotlh rh
![Page 60: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/60.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
2 3 1 4 5 6 8 7
pivotlh rh
![Page 61: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/61.jpg)
int Partition(int arr[], int n){ int lh = 1, rh = n - 1;
int pivot = arr[0]; while (true) { while (lh < rh && arr[rh] >= pivot) rh--; while (lh < rh && arr[lh] < pivot) lh++; if (lh == rh) break; Swap(arr[lh], arr[rh]); } if (arr[lh] >= pivot) return 0; Swap(arr[0], arr[lh]); return lh;}
2 3 1 4 5 6 8 7
pivotlh rh
Returns 4 (index of pivot)
![Page 62: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/62.jpg)
void Quicksort(int arr[], int n){ if (n < 2) return;
int boundary = Partition(arr, n);
// Sort subarray up to pivot Quicksort(arr, boundary);
// Sort subarray after pivot to end Quicksort(arr + boundary + 1, n – boundary - 1);
}
![Page 63: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/63.jpg)
void Quicksort(int arr[], int n){ if (n < 2) return;
int boundary = Partition(arr, n);
// Sort subarray up to pivot Quicksort(arr, boundary);
// Sort subarray after pivot to end Quicksort(arr + boundary + 1, n – boundary - 1);
}
O(1)
O(n)
T(n/2)
T(n/2)
T(n) = O(1) + O(n) + 2T(n/2)= O(n) + 2T(n/2)
Same as MergeSort O(n log n)
![Page 64: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/64.jpg)
The whole recursion
5 3 7 4 8 6 2 1
![Page 65: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/65.jpg)
First partition
2 3 1 4 5 6 8 7
![Page 66: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/66.jpg)
First partition
2 3 1 4 5 6 8 7
![Page 67: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/67.jpg)
Recursive sort {2, 3, 1, 4}
2 3 1 4 5 6 8 7
![Page 68: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/68.jpg)
Partition {2, 3, 1, 4}
1 2 3 4 5 6 8 7
![Page 69: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/69.jpg)
Recursive sort {1}
1 2 3 4 5 6 8 7
![Page 70: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/70.jpg)
Recursive sort {1}
1 2 3 4 5 6 8 7
base case
![Page 71: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/71.jpg)
Recursive sort {3, 4}
1 2 3 4 5 6 8 7
![Page 72: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/72.jpg)
Partition {3, 4}
1 2 3 4 5 6 8 7
![Page 73: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/73.jpg)
Recursive sort {4}
1 2 3 4 5 6 8 7
![Page 74: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/74.jpg)
Recursive sort {4}
1 2 3 4 5 6 8 7
base case
![Page 75: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/75.jpg)
Recursive sort {6, 8, 7}
1 2 3 4 5 6 8 7
![Page 76: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/76.jpg)
Leap of faith!
1 2 3 4 5 6 7 8
![Page 77: Big-O and Sorting February 6, 2006. Administrative Stuff Readings for today: Ch 7.3-7.5 Readings for tomorrow: Ch 8](https://reader036.vdocument.in/reader036/viewer/2022062519/5697bfa61a28abf838c98843/html5/thumbnails/77.jpg)
Empirical comparison of MergeSort vs QuickSort
N Merge sort Quicksort
10 0.54 msec 0.10 msec
20 1.17 msec 0.26 msec
40 2.54 msec 0.52 msec
100 6.90 msec 1.76 msec
200 14.84 msec 4.04 msec
400 31.25 msec 8.85 msec
1000 84.38 msec 26.04 msec
2000 179.17 msec 56.25 msec
4000 383.33 msec 129.17 msec
10,000 997.67 msec 341.67 msec