Quick Sort (11.2) : CSE 2011 Winter 2011
Quick Sort (11.2) : CSE 2011 Winter 2011
2)
CSE 2011
Winter 2011
8 February 2019 1
Quick Sort
Fastest known sorting algorithm in practice
Average case: O(N log N)
Worst case: O(N2)
But the worst case can be made exponentially
unlikely.
Another divide-and-conquer recursive algorithm,
like merge sort.
2
Quick Sort: Main Idea
1. If the number of elements in S is 0 or 1, then
return (base case).
2. Pick any element v in S (called the pivot).
3. Partition the elements in S except v into two
disjoint groups:
1. S1 = {x S – {v} | x v}
2. S2 = {x S – {v} | x v}
4. Return {QuickSort(S1) + v + QuickSort(S2)}
3
Quick Sort: Example
4
Example of Quick Sort...
5
Issues To Consider
How to pick the pivot?
Many methods (discussed later)
How to partition?
Several methods exist.
The one we consider is known to give good results and
to be easy and efficient.
We discuss the partition strategy first.
6
Partitioning Strategy
For now, assume that pivot = A[(left+right)/2].
We want to partition array A[left .. right].
First, get the pivot element out of the way by
swapping it with the last element (swap pivot and
A[right]).
Let i start at the first element and j start at the next-to-
last element (i = left, j = right – 1)
swap
5 7 4 6 3 12 19 5 7 4 19 3 12 6
pivot i j
7
Partitioning Strategy
Want to have
A[k] pivot, for k < i pivot pivot
5 7 4 19 3 12 6 5 7 4 19 3 12 6
i j i j 8
Partitioning Strategy (2)
When i and j have stopped and i is to the left of j (thus
legal)
Swap A[i] and A[j]
The large element is pushed to the right and the small element
is pushed to the left
After swapping
A[i] pivot
A[j] pivot
Repeat the process until i and j cross
swap
5 7 4 19 3 12 6 5 3 4 19 7 12 6
i j i j 9
Partitioning Strategy (3)
5 3 4 19 7 12 6
When i and j have crossed
swap A[i] and pivot
i j
Result:
5 3 4 19 7 12 6
A[k] pivot, for k < i
A[k] pivot, for k > i
j i
swap A[i] and pivot
5 3 4 6 7 12 19
Break! 10
j i
Picking the Pivot
Median-of-three partitioning
eliminates the bad case for sorted input.
reduces the number of comparisons by 14%.
13
Median of Three Method
Compare just three elements: the leftmost, rightmost and center
Swap these elements if necessary so that
A[left] = Smallest
A[right] = Largest
A[center] = Median of three
Pick A[center] as the pivot.
Swap A[center] and A[right – 1] so that the pivot is at the second last
position (why?)
14
Median of Three: Example
A[left] = 2, A[center] = 13,
2 5 6 4 13 3 12 19 6
A[right] = 6
pivot
pivot
We only need to partition A[ left + 1, …, right – 2 ]. Why? 15
Quick Sort Summary
Recursive case: QuickSort( a, left, right )
pivot = median3( a, left, right );
Partition a[left … right] into a[left … i-1], i, a[i+1 … right];
QuickSort( a, left, i-1 );
QuickSort( a, i+1, right );
Choose pivot
Partitioning
Recursion
inner loop
21
Analysis
Assumptions:
A random pivot (no median-of-three partitioning)
No cutoff for small arrays ( to make it simple)
23
Worst-Case Scenario
What will be the worst case?
The pivot is the smallest element, all the time
Partition is always unbalanced
24
Best-Case Scenario
What will be the best case?
Partition is perfectly balanced.
Pivot is always in the middle (median of the array).
25
Average-Case Analysis
Assume that each of the sizes for S1 is equally
likely has probability 1/N.
This assumption is valid for the pivoting and
partitioning strategy just discussed (but may not
be for some others),
On average, the running time is O(N log N).
Proof: pp 272–273, Data Structures and
Algorithm Analysis by M. A. Weiss, 2nd edition
26
Next time …
Arrays (review)
Linked Lists (3.2, 3.3)
Comparing sorting algorithms
Stacks, queues (Chapter 5)
27