Computer ScienceAlgorithmsMedium

Quick Sort

Also known as:Partition-exchange sort

Quick sort is a highly efficient, in-place divide-and-conquer sorting algorithm that selects a "pivot" element and partitions the array into two sub-arrays — elements less than the pivot and elements greater than the pivot — then recursively sorts each sub-array. It achieves an average time complexity of O(n log n) and is often faster in practice than merge sort due to better cache performance and lower constant factors. Quick sort is widely used in standard library implementations, including the C++ STL and Java's Arrays.sort for primitive types.

Key Formula

T(n) = T(k) + T(n - k - 1) + O(n)

LaTeX: T(n) = T(k) + T(n - k - 1) + O(n)

SymbolMeaningUnit
T(n)Time to sort n elementsoperations
kNumber of elements in the left partitionelements
n - k - 1Number of elements in the right partitionelements
O(n)Time to partition the arrayoperations

Worked Example

Problem

Sort [10, 80, 30, 90, 40, 50, 70] using quick sort with the last element as pivot.

Solution

Step 1: Pivot = 70. Partition: elements < 70 go left, > 70 go right → [10, 30, 40, 50, 70, 80, 90], pivot at index 4. Step 2: Recursively sort left sub-array [10, 30, 40, 50]: pivot = 50, partition → [10, 30, 40, 50]. Step 3: Recursively sort [10, 30, 40]: pivot = 40 → [10, 30, 40]. Step 4: Right sub-array [80, 90]: pivot = 90 → [80, 90]. Step 5: Combine all.

Answer

Sorted array: [10, 30, 40, 50, 70, 80, 90].

Quick Sort Complexity and Properties

PropertyValueNotes
Best Case TimeO(n log n)Pivot always splits evenly
Average Case TimeO(n log n)Random pivot selection
Worst Case TimeO(n²)Already sorted, bad pivot
Space ComplexityO(log n)Call stack depth
StabilityUnstableRelative order not preserved
In-placeYesNo auxiliary array needed

Interactive Tools

VisuAlgo – Sorting

Animated quick sort with partition steps

Open Tool

Khan Academy – Quick Sort

Quick sort explanation and analysis

Open Tool

Brilliant.org

Quick sort analysis and pivot strategies

Open Tool
Diagram of the quick sort partitioning process

Wikimedia Commons, CC BY-SA

Related Terms

Quick sort was developed by British computer scientist Tony Hoare in 1959 while working as an exchange student at Moscow State University, and published in 1961. Hoare chose the name to reflect the algorithm's empirically fast performance relative to other sorting methods of the time.

quick-sortsortingdivide-and-conquerin-placealgorithmspivot