View
218
Download
0
Tags:
Embed Size (px)
Citation preview
CS2420: Lecture 11
Vladimir KulyukinComputer Science Department
Utah State University
Outline
• Sorting Algorithms (Chapter 7)
QuickSort: Sorting in Place• N elements are partitioned into three
segments: Left, Pivot, and Right.
• The Pivot segment contains exactly one element.
• No element in the Left segment is larger than the Pivot.
• No element in the Right segment is smaller than the Pivot.
Quicksort
Array A
Quicksort
Array A
Pivot
Quicksort
Array A
Pivot
Left Right
Quicksort: Key Insight
• The keys to the left of the pivot are less than or equal to the pivot.
• The keys to the right of the pivot are greater than or equal to the pivot.
• Hence, the left and right segments can be sorted independently and no merge is required.
Quicksort: Example
13, 81, 92, 43, 65, 31, 57, 26, 75, 0
13, 0, 26, 43, 57, 31 65 92, 75, 81
Partition
65 is chosen to be the pivot.
Quicksort’s Correctness
• After the partition the pivot v will be in A[i].
• The elements in sub-array L are sorted recursively.
• The elements in sub-array R are sorted recursively.
• No element in L is larger than v = A[i].
• No element in R is smaller than v = A[i].
QuickSort: Pseudocode
void quickSort(A, int left, int right) {if ( right – left + 1 <= 3 )
sort A directly;else {
v = choose_pivot(A, left, right);i = partition(A, left, right, v);// i is the position of the pivot after// the partition is donequickSort(A, left, i-1);quickSort(A, i+1, right);
}}
Quicksort: 3 Basic Questions
• Q1: How do we choose the pivot?
• Q2: How do we perform the partition?
• Q3: How do we treat elements equal to the pivot?
Q1: Picking a Pivot
• We should not pick a pivot near the front or end of the array.
• The middle element is a reasonable, but passive choice.
• Median-of-three partitioning is a reliable choice in practice.
Definition
number.largest
2 theis numbers ofmedian A thN
N
Median-Of-Three Partitioning
Choose the pivot to be the median of the first, middle, and last elements of the array.
Q2: Partitioning Strategy
• Swap the pivot v with the last element in the array.
• Run index i from left to right.• Run index j from right to left.• When A[i] >= v, stop incrementing i.• When A[j] <= v, stop decrementing j.• If i and j have not crossed, swap(A[i], A[j]) and
continue.• If i and j have crossed, swap the pivot with A[i].
Partitioning: Example
8, 1, 4, 9, 6, 3, 5, 2, 7, 0
8, 1, 4, 9, 0, 3, 5, 2, 7, 6
i j
8, 1, 4, 9, 0, 3, 5, 2, 7, 6
i j
A[i] >= 6 so i is stopped. A[j] > 6 so j can be decremented.
A[j] <= 6 so j can be stopped.
Now we can swap A[i] and A[j].
Partitioning: Example
2, 1, 4, 9, 0, 3, 5, 8, 7, 6
i j
2, 1, 4, 5, 0, 3, 9, 8, 7, 6
ij
2, 1, 4, 5, 0, 3, 6, 8, 7, 9
i and j crossed, so we stop.
Finally we swap the pivot with A[i].
Partitioning: Asymptotic Analysis
.Partition NON
Q3: Equal Elements
• Elements equal to the pivot are swapped.
• This seems to be redundant, but turns out to be the best choice in practice since we do not have to deal with a number of special cases. For example, we do not have to change our algorithm for arrays of identical elements.
Quicksort: Observations
• Like MergeSort, Quicksort is a divide-and-conquer comparison sort.
• Unlike Mergesort, Quicksort does not allocate any extra arrays.
QuickSort: Recurrence
NLeftNTLeftTnT 1||||
Worst-Case Analysis
• In the worst case, we always pick the pivot as the smallest or largest element in the array.
• In this case, we break the basic recursive design principle to partition the problem into chucks of equal size.
• The array is reduced only by 1 element.
Worst-Case Analysis: Recurrence
2
1
nOnT
nnTnT