Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Quicksort

Быстрая сортировка

This sorting algorithm picks a value called a pivot and groups items by how they compare with it. It then repeats the process on the groups that still need sorting.

Partition first, sort the pieces second

Quicksort is a divide-and-conquer algorithm. Its key step, called partitioning, rearranges a section of an array around a chosen value called the pivot. In a three-way partition, values below the pivot go on the left, values equal to it go in the middle, and values above it go on the right. The outer groups may still be scrambled, but no item on the left needs to cross to the right again. Sort the outer groups independently and you're done: there is no merge step.

A small example

Take [6, 2, 6, 9, 1] and pick 6 as the pivot. One valid result of partitioning is [2, 1 | 6, 6 | 9]. The two sixes need no further work, and the group containing 9 is already sorted because it has only one item. The only group left to sort is [2, 1].

The following Python implementation rearranges the array in place. It takes inclusive endpoints; pass 0 and len(a) - 1 to sort the entire array.

def quicksort(a, lo, hi):
    if lo >= hi:
        return
    pivot = a[(lo + hi) // 2]
    lt, i, gt = lo, lo, hi
    while i <= gt:
        if a[i] < pivot:
            a[i], a[lt] = a[lt], a[i]
            lt += 1
            i += 1
        elif a[i] > pivot:
            a[i], a[gt] = a[gt], a[i]
            gt -= 1
        else:
            i += 1
    quicksort(a, lo, lt - 1)
    quicksort(a, gt + 1, hi)

The unexamined region runs from i through gt. When an item is swapped in from gt, it has not been classified yet, so i stays put. When the loop finishes, the equal group runs from lt through gt, and both recursive calls skip it.

Performance and trade-offs

Partitioning m items takes Θ(m) time. With roughly balanced splits, there are O(log n) levels of work, each processing at most n items. Picking a fresh pivot uniformly at random in each subarray gives O(n log n) expected time. The worst case is still Θ(n²): with distinct keys, repeatedly picking the smallest or largest item leaves just one nonempty subarray, one item shorter than before. Picking the middle index, as the example does, is not the same as finding the median value.

Quicksort is a good fit for arrays when you want to rearrange items without allocating a second array. In-place partitioning does not mean the entire sort uses constant extra space: the recursive implementation above can use O(n) stack space in the worst case. It is also not stable. If two records have the same sort key, swaps may reverse their original order. A stable implementation of merge sort is an alternative when that order matters.

What interviewers look for

State what each index means and which endpoints are inclusive before writing the recursive calls. Then trace the code on an empty array and an array of identical values. Those cases show whether the base case works and whether the recursive calls actually shrink the problem.