Quick Sort: Algorithms and Data Structures
Quick Sort: Algorithms and Data Structures
Quick Sort: Algorithms and Data Structures
Dino KECO
Agenda
quick sort
selection
duplicate keys
system sorts
Two classic sorting algorithms:
merge sort and quick sort
Critical components in the world’s computational infrastructure.
Full scientific understanding of their properties has enabled us to
develop them into practical system sorts.
Quick sort honored as one of top 10 algorithms of 20th century in
science and engineering.
Quick Sort
Basic plan.
Shuffle the array.
Partition so that, for some j
entry a[j] is in place
This was Sadgewick 2-way partitioning, there are other strategies like
Dijkstra 3-way partitioning
Bentley-McIlroy 3-way partitioning
dual-pivot partitioning
Quick Sort: Java code for
partitioning
Quick Sort: Java implementation
Quick Sort: implementation details
Best case.
Number of compares is ~ N log N.
Worst case.
Number of compares is ~ ½ N2 .
Quick Sort: practical
improvements
Insertion sort small subarrays.
Even quick sort has too much overhead for tiny
subarrays.
Cutoff to insertion sort for ≈ 10 items
Median of sample.
Best choice of pivot item = median.
Estimate true median by taking median of
sample.
Median-of-3 (random) items
Selection
Goal. Given an array of N items, find the k th smallest item.
Ex. Min (k = 0), max (k = N - 1), median (k = N/ 2).
Applications.
Order statistics.
Which is true?
N log N lower bound?
N upper bound?
Duplicate keys
Data compression.
Computer graphics.
Computational biology.
Load balancing on a parallel computer.
...
Questions?