











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
In all programming language only syntax is different not the logic. This course discuss core concepts for many different programming language and techniques. Key points for this lecture are: Quicksort, Partitioning, Partition Method, Quicksort Method, Analysis of Quicksort, Partitioning at Various Levels, Worst Case, Worst Case for Quicksort, Typical Case for Quicksort, Tweaking Quicksort
Typology: Slides
1 / 19
This page cannot be seen from the preview
Don't miss anything!












docsity.com
1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right]
docsity.com
Choose an array value (say, the first) to use as the pivot
Starting from the left end, find the first element that is greater than or equal to the pivot
Searching backward from the right end, find the first element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until done
docsity.com
To partition a[left...right]:
docsity.com
static int partition(int[] a, int left, int right) { int p = a[left], l = left + 1, r = right; while (l < r) { while (l < right && a[l] < p) l++; while (r > left && a[r] >= p) r--; if (l < r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } } a[left] = a[r]; a[r] = p; return r; }
docsity.com
static void quicksort(int[] array, int left, int right) { if (left < right) { int p = partition(array, left, right); quicksort(array, left, p - 1); quicksort(array, p + 1, right); } }
docsity.com
docsity.com
We cut the array size in half each time
So the depth of the recursion in log 2 n
At each level of the recursion, all the partitions at that level do work that is linear in n
O(log 2 n) * O(n) = O(n log 2 n)
Hence in the average case, quicksort has time complexity O(n log 2 n)
What about the worst case?
docsity.com
docsity.com
In the worst case, recursion may be n levels deep (for an array of size n)
But the partitioning work done at each level is still n
O(n) * O(n) = O(n^2 )
So worst case for Quicksort is O(n^2 )
When does this happen?
There are many arrangements that could make this happen Here are two common cases: When the array is already sorted When the array is inversely sorted (sorted in the opposite order)
docsity.com
Almost anything you can try to “improve” Quicksort will actually slow it down
One good tweak is to switch to a different sorting method when the subarrays get small (say, 10 or 12) Quicksort has too much overhead for small array sizes
For large arrays, it might be a good idea to check beforehand if the array is already sorted But there is a better tweak than this
docsity.com
Before, we picked the first element of the subarray to use as a pivot If the array is already sorted, this results in O(n^2 ) behavior It’s no better if we pick the last element
We could do an optimal quicksort (guaranteed O(n log n)) if we always picked a pivot value that exactly cuts the array in half Such a value is called a median: half of the values in the array are larger, half are smaller The easiest way to find the median is to sort the array and pick the value in the middle (!)
docsity.com
Quicksort is the fastest known sorting algorithm
For optimum efficiency, the pivot must be chosen carefully
“Median of three” is a good technique for choosing the pivot
However, no matter what you do, there will be some cases where Quicksort runs in O(n^2 ) time
docsity.com