Skip to content

Array Partition

Rearrange elements so that those satisfying a predicate appear before others.

Array partition rearranges elements so that all elements satisfying a predicate come before those that do not. The operation runs in-place and does not require additional storage.

You use it as a primitive in selection, quicksort, filtering, and grouping tasks.

Problem

Given an array AA of length nn and a predicate P(x)P(x), reorder the array so that:

  • for all indices i<ki < k, P(A[i])P(A[i]) holds
  • for all indices iki \ge k, P(A[i])P(A[i]) does not hold

for some partition index kk.

Algorithm

Use a single pass with a boundary pointer.

partition(A, P):
    k = 0

    for i from 0 to length(A) - 1:
        if P(A[i]):
            swap A[i], A[k]
            k += 1

    return k

Example

Let

A=[8,3,5,1,9] A = [8, 3, 5, 1, 9]

Partition by even numbers:

P(x):xmod2=0 P(x): x \bmod 2 = 0
stepiactionarrayk
108 even, swap with A[0][8, 3, 5, 1, 9]1
213 odd[8, 3, 5, 1, 9]1
325 odd[8, 3, 5, 1, 9]1
431 odd[8, 3, 5, 1, 9]1
549 odd[8, 3, 5, 1, 9]1

Result:

[83,5,1,9] [8 \mid 3, 5, 1, 9]

Partition index:

k=1 k = 1

Correctness

At each step:

  • indices [0,k1][0, k-1] contain elements that satisfy PP
  • indices [k,i1][k, i-1] contain elements that do not satisfy PP

When P(A[i])P(A[i]) holds, swapping it into position kk extends the valid prefix. The invariant is preserved after each iteration. At the end, all elements before kk satisfy the predicate, and all others follow.

Complexity

operationtime
partitionO(n)O(n)

Space usage:

O(1) O(1)

When to Use

Array partition is appropriate when:

  • elements must be grouped by a condition
  • in-place rearrangement is required
  • order inside groups does not matter
  • used as a step in quicksort or selection

It is less suitable when:

  • stability is required
  • original order must be preserved

Implementation

def partition(a, predicate):
    k = 0
    for i in range(len(a)):
        if predicate(a[i]):
            a[i], a[k] = a[k], a[i]
            k += 1
    return k
func Partition[T any](a []T, pred func(T) bool) int {
    k := 0
    for i := 0; i < len(a); i++ {
        if pred(a[i]) {
            a[i], a[k] = a[k], a[i]
            k++
        }
    }
    return k
}