Quicksort
Objectives
We would like to :- Investigate Quicksort
Notes
- Note, Quicksort is actually chapter 7
- Quicksort was published in 1961.
- The basic idea:
- Partition the data into a "small" and "large" set about some pivot point.
- Sort the small and large partitions.
- It does not require extra space to perform this action.
-
QUICK-SORT(A, start, end) Input : An array A index from 0 to A.size-1 start the first position to start sorting end the last position to sort Output: A will be ordered from start to end- if start < end
- pivot ← partition(A, start, end)
- Quicksort(A, start, pivot-1)
- Quicksort(A, pivot+1, end)
- The big issue is the partition algorithm
- There are at least two out there.
- The Hoare method named after the inventor of the quicksort algorithm was used for years.
- The Lomuto method is easier to understand an prove.
- There are at least two out there.
- Lomuto's Partition
-
PARTITION(A, start end) Input: an unordered Array from start to end Output: a position p that divides the array A[i] ≤ A[p] for all i < p A[i] ≥ A[p] for all i > p- pivotValue ← A[end]
- pivotPoint ← start-1
- for i ← start to end -1
- if A[i] ≤ poviotValue
- pivotPoint ← pivotPoint + 1
- swap(A[i], A[pivotPoint];
- swap(A[end], A[pivotPoint+1];
- Let's trace this with {3, 7, 9, 2, 5, 1, 4}
-
pivotValue PivotPoint i A 0 1 2 3 4 5 6 4 -1 0 3 7 9 2 5 1 _ 4 is still at position 6 3 < 4, so swap 4 -1 1 3 7 9 2 5 1 _ 3 and 3 exchanged At this point notice The array less than 1 is small 4 0 1 3 7 9 2 5 1 _ 7 > 4, no action 4 0 2 3 7 9 2 5 1 _ 9 > 4, no action 4 0 3 3 7 9 2 5 1 _ 2 < 4, swap 4 1 3 3 2 9 7 5 1 _ 2 and 7 exchanged {3, 2} are less than the pivot value {9, 7} are greater than the pivot value {5, 1} are unknown 4 2 4 3 2 9 7 5 1 _ 5 > 4, no action 4 2 5 3 2 9 7 5 1 _ 1 < 4, swap 4 3 5 3 2 1 7 5 9 _ 1 and 9 exchanged At this point note The array [start, pivotPoint-1] is less than the pivot value The array [pivotPoint, end-1] is greater than the pivot point A[pivotPoint] can go to the end of the array where the pivot value is stored 4 3 5 3 2 1 4 5 9 7 - We can argue that this works
-
In the beginning, the array less that pivot point (empty) contains values less than the pivot point The array pivotpoint and greater (the entire array) contains unclassified values
-
-
- What does Lomuto's partition algorithm do?
- It partitions the array into two parts, a portion smaller than the partition and a part greater than the partition by maintaining the array in three portions
- A portion smaller than the partition
- A portion that is larger than the partition
- A portion that is unclassified.
- As we move through the array (line 4), if we encounter a value less than the partition, we will
- Move the divider between the smaller and larger portion to the right by one (line 5)
- Move the data value to the new "last" position in the small array. (line 6)
- Moving the smaller, we exchange this with either it's self (first position smaller) or something either unordered or larger
- This depends on if there are elements in the larger portion.
- In either case,
- The small portion grows by one.
- In any case, the unordered portion decreases by one.
- And the pivot position always points to the first element of either the unordered portion or the larger portion.
- In the end, the pivot element, the last element in the array is swapped into the first position of the larger list and the first element of the larger list is moved to the end of the array.
- Thus the array to the left than the pivot element is smaller than the pivot element and the portion to the right of the pivot element is smaller than the pivot element.
- It partitions the array into two parts, a portion smaller than the partition and a part greater than the partition by maintaining the array in three portions
- Performance
- O(n).
- Next we will discuss the performance of Quicksort.