Menu
Coddy logo textTech

Quick Sort

Last updated

Quicksort is a divide-and-conquer algorithm that sorts around a "pivot". It picks a pivot element, then partitions the array so everything smaller comes before it and everything larger comes after - which locks the pivot into its final sorted position. It then recurses on the left and right partitions. This visualization uses the Lomuto scheme with the last element as pivot. Press play to watch the partitioning and pivot placement.

Quicksort is usually the fastest general-purpose sort in practice thanks to good cache behavior and in-place partitioning, averaging O(n log n). Its worst case is O(n²) (e.g. an already-sorted array with a poor pivot choice), which good pivot strategies like median-of-three or randomization avoid.

Time & space complexity

CaseComplexityNotes
Best caseO(n log n)Balanced partitions
Average caseO(n log n)Random order
Worst caseO(n²)Consistently unbalanced pivots
SpaceO(log n)Recursion stack (in-place partition)
StableNoPartition swaps reorder equal elements

Step by step

StepWhat happens
1Choose a pivot (here, the last element of the range).
2Partition: move all elements smaller than the pivot to its left.
3Swap the pivot into the boundary - it is now in final position.
4Recursively quicksort the left partition.
5Recursively quicksort the right partition.

Worked example

Sorting [5, 2, 4, 1] with the Lomuto scheme (last element as pivot):

PassArrayAction
Start[5, 2, 4, 1]Partition the whole range; pivot is 1 (last element).
1[1, 2, 4, 5]Nothing is smaller than 1, so swap 1 into index 0; pivot 1 is now final. Recurse right on [2, 4, 5].
2[1, 2, 4, 5]Partition [2, 4, 5] with pivot 5; both 2 and 4 are smaller, so 5 stays at the end and is final. Recurse left on [2, 4].
3[1, 2, 4, 5]Partition [2, 4] with pivot 4; 2 is smaller, so 4 stays put and is final. 2 is a single element, so it is already sorted.
Done[1, 2, 4, 5]Every pivot is locked in place; the array is sorted.

When to use quicksort

Use it whenAvoid it when
You need a fast, general-purpose in-memory sort with small constant factors.You need guaranteed O(n log n) worst-case time (use heap sort or merge sort).
Memory is tight - partitioning is in place and needs only O(log n) stack space.You need a stable sort that preserves the order of equal keys.
Data is in a random or unknown order and you use a randomized or median-of-three pivot.The input is already sorted or nearly sorted and the pivot is fixed, triggering O(n²).
Good cache locality matters, since quicksort accesses memory sequentially.You are sorting a linked list, where merge sort avoids the random access quicksort relies on.

Quick Sort code

A clean, runnable Quick Sort implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Quick Sort code in Python

Python
1def quick_sort(a, low=0, high=None):2    if high is None:3        high = len(a) - 14    if low < high:5        p = partition(a, low, high)6        quick_sort(a, low, p - 1)7        quick_sort(a, p + 1, high)8    return a9
10
11def partition(a, low, high):12    # Lomuto partition: everything < pivot moves left of it13    pivot = a[high]14    i = low15    for j in range(low, high):16        if a[j] < pivot:17            a[i], a[j] = a[j], a[i]18            i += 119    a[i], a[high] = a[high], a[i]20    return i21
22
23nums = [10, 7, 8, 9, 1, 5]24print("Before:", nums)25quick_sort(nums)26print("After: ", nums)
Run this code in the Python Playground

Quick Sort FAQ

What is the time complexity of quicksort?
Quicksort averages O(n log n) and is O(n log n) in the best case, but degrades to O(n²) in the worst case when partitions are consistently unbalanced. Randomized or median-of-three pivots make the worst case very unlikely.
Is quicksort stable?
No. The standard in-place partition swaps distant elements, which can change the relative order of equal keys. Stable variants exist but give up quicksort’s in-place advantage.
Why is quicksort often faster than merge sort?
Quicksort partitions in place with excellent cache locality and no extra buffer, so its constant factors are small. Merge sort matches its O(n log n) bound but pays for an O(n) buffer and more data movement.
Quicksort vs merge sort - which should I use?
Choose quicksort for fast in-place sorting of arrays in memory, where its small constants usually win. Choose merge sort when you need a stable sort, a guaranteed O(n log n) worst case, or you are sorting linked lists or external data that does not fit in RAM.
Why does quicksort become O(n²) on a sorted array?
With a fixed pivot such as the first or last element, an already-sorted input makes every partition split off just one element, producing n levels of recursion instead of log n. Choosing the pivot randomly or with median-of-three breaks this pattern and restores O(n log n) behavior.
What is the difference between the Lomuto and Hoare partition schemes?
The Lomuto scheme uses a single index scanning left to right and is simpler to code, which is why this visualization uses it. The Hoare scheme uses two pointers moving inward and typically does fewer swaps, making it faster in practice, but it does not place the pivot in its final position during the partition step.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED