QUICKSORT
BASIC IDEA
Pick one element in the array, which
will be the pivot.
Make one pass through the array,
called a partition step, re-arranging
the entries so that:
• entries smaller than the pivot are to
the left of the pivot.
• entries larger than the pivot are to
the right
BASIC IDEA
Recursively apply quicksort to the part
of the array that is to the left of the
pivot, and to the part on its right.
No merge step (like merge sort), at the
end all the elements are in the proper
order
TWO KEY STEPS
How to pick a pivot?
How to partition?
PARTITIONING (QUICKSORT )
A key step in the Quicksort algorithm is
Partitioning the array
⚫ We choose some (any) number p in
the array to use as a pivot
⚫ We partition the array into three
parts:
numbers p numbers greater than
less than p or equal to p 5
EXAMPLE OF PARTITIONING
choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
search: 436924312189356
swap: 433924312189656
search: 433924312189656
swap: 433124312989656
search: 433124312989656
swap: 433122314989656
search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (L > R)
swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
6
ANALYSIS OF QUICKSORT—BEST
CASE
Suppose each partition operation
divides the array almost exactly in two
half.
Then the depth of the recursion in
log2n (like merge sort)
⚫ Because that’s how many times we can
halve n
7
PARTITIONING AT VARIOUS LEVELS
8
BEST CASE II
We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the
partitions at that level do work that is
linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quicksort has
time complexity O(n log2n)
What about the worst case?
9
ANALYSIS OF QUICKSORT—WORST
CASE
In the worst case, partitioning always
divides the size n array into these three
parts:
⚫ A length one part, containing the pivot
itself
⚫ A length zero part, and
⚫ A length n-1 part, containing
everything else
We don’t recur on the zero-length part
Recurring on the length n-1 part requires10
(in the worst case) recurring to depth n-1
WORST CASE PARTITIONING
11
WORST CASE FOR QUICKSORT
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(n2)
So worst case for Quicksort is O(n2)
When does this happen?
⚫ When the array is sorted to begin with!
12
TYPICAL CASE FOR QUICKSORT
If the array is sorted to begin with,
Quicksort is terrible: O(n2)
However, Quicksort is usually O(n log2n)
The constants are so good that Quicksort
is generally the fastest algorithm known
Most real-world sorting is done by
Quicksort
13
CHOOSING THE PIVOT
Some fixed element: e.g. the first, the last,
the one in the middle.
Bad choice –
If the array is already sorted, this
results in O(n2) behavior.
It’s no better if we pick the last element.
Randomly chosen (by random generator)
- still a bad choice
CHOOSING THE PIVOT
The median of the array
(if the array has N numbers, the median is
the [N/2] largest number).
This is difficult to compute - increases the
complexity.
CHOOSING THE PIVOT
The median-of-three choice:
take the first, the last and the middle
element.
Choose the median of these three
elements.
FINAL COMMENTS
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(n2) time
17
COMPARISON WITH MERGE
SORT
Merge sort guarantees O(NlogN) time
Merge sort requires additional
memory with size N
Usually Merge sort is not used for main
memory sorting, only for external
memory sorting