3-Way Quick Sort
- Ω( n )
- Θ( n log n )
- O( n2 )
- space O( log n )
Array
Comparator strip
Index down, time to the right
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How 3-way quick sort works
Three-way quick sort splits the array into three parts around the pivot: smaller, equal and larger. It is Dijkstra's Dutch national flag partition applied to quick sort.
Read more
Elements equal to the pivot are never touched again, so arrays with many duplicates sort in close to linear time, where a plain two-way quick sort degrades. The pivot is the median of the first, middle and last element.
| Best time | Ω( n ) | good |
|---|---|---|
| Average time | Θ( n log n ) | good |
| Worst time | O( n2 ) | poor |
| Worst space | O( log n ) | good |
Big O notation gives the lower, average and upper bounds of an algorithm. Time complexity bounds the number of operations; space complexity bounds the extra memory it uses.