Reading preferences
Concept

Partitioning and Quickselect

Divide values around a pivot, then keep only the side containing a wanted rank.

partition, quicksort, quickselect, selection, pivot

Find a place, not a fully sorted row

Suppose you want the third-smallest card, not the whole row in order. Pick a pivot and divide the cards into smaller, equal, and larger groups. If the third position belongs to the smaller group, the larger group cannot contain the answer.

This is selection by rank. Our example uses zero-based rank: rank 0 means the smallest value. Quicksort instead sorts both sides.

Why use it and where it helps

Selecting a rank helps find a median or a cutoff without necessarily sorting everything. Three-way partitioning is useful with many duplicate values. Different partition schemes and pivot choices have different trade-offs; no single fixed pivot guarantees balanced groups.

Follow an example

Find rank 2 in [4, 1, 3, 2, 3], with pivot 4:

smaller [1, 3, 2, 3] | equal [4] | larger []

Rank 2 lies in the four-item smaller group. Choosing 1 next leaves no smaller items, one equal item, and [3, 2, 3] larger; the wanted rank there becomes

  1. Pivot 3 gives smaller [2] and equal [3, 3], so rank 1 is 3.

Two-way quicksort partitions around a pivot into two sides; three-way quicksort explicitly groups equals. Randomising pivot choices can improve expected behaviour for a fixed input, but it does not eliminate the worst case.

See the reasoning

Follow the example one step at a time

Find zero-based rank 2 without sorting the entire row. This is a prepared teaching demonstration, not live code execution.

Start

values: [4, 1, 3, 2, 3]   wanted rank: 2

Rank 2 means the third-smallest value.

Python illustration

def quickselect(numbers, rank):
    if not 0 <= rank < len(numbers):
        raise ValueError("rank must identify an item")
    items = numbers[:]
    while True:
        pivot = items[0]
        smaller = [x for x in items if x < pivot]
        equal = [x for x in items if x == pivot]
        larger = [x for x in items if x > pivot]
        if rank < len(smaller):
            items = smaller
        elif rank < len(smaller) + len(equal):
            return pivot
        else:
            rank -= len(smaller) + len(equal)
            items = larger


print(quickselect([4, 1, 3, 2, 3], 2))
print(quickselect([7], 0))

Prepared output — not live execution:

3
7

This function assumes an integer rank and ordinary comparable numbers with consistent ordering, excluding values such as NaN. It raises ValueError for an out-of-range rank and does not change the input.

Costs and common mistakes

With well-balanced partitions, selection processes a shrinking sequence of groups in O(n) total time. A uniformly random pivot gives expected O(n) under the usual comparison model; our fixed first pivot has no such expectation guarantee and can take O(n²) on adversarial input. This implementation keeps copied groups, using O(n) peak extra space. Partitioning alone does not sort either side, and duplicate values occupy multiple ranks.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does partitioning arrange relative to a pivot?
2. Why can a three-way partition help with repeated values?
3. How is quickselect different from quicksort?
4. Can repeatedly poor pivots make quickselect quadratic?

References and further reading