Quicksort

An efficient, comparison-based divide-and-conquer sorting algorithm that partitions an array around a pivot and recursively sorts the subarrays.

Method

Choose pivot; partition so elements ≤ pivot are on left, ≥ on right; recurse. Average complexity O(n log n); worst-case O(n²) on already sorted data with poor pivot choice. Randomized or median-of-three pivots mitigate worst case. In-place version is common.

Applications

General-purpose sorting in standard libraries, database query processing, programming language runtimes, educational exemplar of divide-and-conquer.

Advantages

Excellent average-case performance; in-place; practical constants are low; widely optimized in libraries.

Limitations

Worst-case time is O(n²). Randomized pivots reduce the probability of bad partitions but do not remove the worst case; introsort adds a worst-case fallback.

Alternatives

Mergesort (stable, guaranteed O(n log n)), Heapsort, Timsort (Python/Java), radix sort for integers, parallel sorting networks.

Example

Partition [5, 2, 8, 1] around 5, then recursively sort [2, 1] and [8] to obtain [1, 2, 5, 8].

Editorial clarification

Corrected or qualified against standard references; bundled concepts are distinguished.

Sources

Educational reference only, not professional advice.

All algorithms