AlgoViz

Quick sort

medium 18 min

Quick sort picks a pivot, partitions the array so smaller values sit to its left and larger values to its right, and then sorts each side the same way.

Where it shows up

The same pattern appears across everyday software.

  • C++ std::sort

    The major standard libraries implement std::sort as introsort: quick sort with median-of-three pivots, a switch to heap sort if recursion gets too deep, and insertion sort for small ranges.

  • Java's Arrays.sort for primitives

    Sorting int[] or double[] uses a dual-pivot quick sort, which partitions into three parts around two pivots. Objects use a stable merge-based sort instead, because quick sort is not stable.

  • Go and Rust unstable sorts

    Go's sort package (since Go 1.19) uses pattern-defeating quick sort (pdqsort), and Rust's sort_unstable is built on the same ideas: a quick sort that detects bad pivots and already sorted runs and falls back to heap sort.

  • C's qsort and database engines

    The C standard library function is named after quick sort, and many in-memory sort routines in databases use quick sort variants for sorting rows that fit in memory before merging runs on disk.

  • Quickselect for medians and top-k

    The same partition step finds the k-th smallest value in O(n) average time. NumPy's partition and C++ std::nth_element use it, and it powers median filters and percentile queries.