Quick Sort Explained: Partition Traces, Complexity and GATE Practice

Trace Quick Sort step by step, distinguish its partition schemes, derive its time and space costs, and solve the question forms that commonly test the concept.

KnowledgeGate Team

Exam prep & CS education

Updated 1 Dec 20266 min read
Quick Sort Explained: Partition Traces, Complexity and GATE Practice

Quick Sort's idea, choose a pivot and partition, is easy to remember. Questions become difficult when they ask for the array after one call, the returned index, or the recursion shape. Most mistakes come from switching partition contracts. For GATE CS preparation, identify the partition contract before tracing Lomuto or Hoare, deriving complexity, or solving practice checks.

Quick Sort in one mental model

Quick Sort is a comparison-based, divide-and-conquer method: choose a pivot, partition the current subarray, then recursively sort both sides.

Partitioning does not fully sort the subarray. After the first Lomuto partition of [9, 3, 7, 1, 8, 2, 5], only pivot 5 is guaranteed to be in its final position. The sides [3, 1, 2] and [8, 7, 9] still need work.

Recursion stops when low >= high. If k elements land left of the pivot, T(n) = T(k) + T(n-k-1) + Θ(n), where the final term is the partition scan. For a cross-algorithm table of time, space and stability, use Sorting Algorithms: Complexity and Comparison; the sections below stay with Quick Sort's partition contracts, traces and recursion shapes.

Lomuto and Hoare are different contracts

For last-element Lomuto, set pivot = A[high] and i = low - 1. Scan j from low to high - 1. If A[j] <= pivot, increment i and swap A[i] with A[j]. Finally, swap A[i+1] with A[high] and return q = i+1.

Before each comparison, its invariant is:

  • A[low..i] <= pivot

  • A[i+1..j-1] > pivot

  • A[j..high-1] is unclassified

The pivot is fixed at q; recurse on [low, q-1] and [q+1, high].

Hoare returns a boundary instead. Set i = low - 1 and j = high + 1. Move i right until A[i] >= pivot, move j left until A[j] <= pivot, and swap unless the pointers have crossed. On [4, 9, 4, 2, 7, 3] with pivot value 4, the scans swap indices 0 and 5, producing [3, 9, 4, 2, 7, 4], then indices 1 and 3, producing [3, 2, 4, 9, 7, 4]. The pointers cross and return j = 2. Recurse on [low, j] and [j+1, high]; j need not be the pivot's final index. Never mix the two schemes' bounds.

Lomuto partition trace

Start with A = [9, 3, 7, 1, 8, 2, 5], low = 0, high = 6, pivot = 5, and i = -1.

  1. At j = 0, 9 > 5, so nothing moves.

  2. At j = 1, 3 <= 5. Set i = 0 and swap 9 with 3: [3, 9, 7, 1, 8, 2, 5].

  3. At j = 2, 7 > 5, so nothing moves.

  4. At j = 3, 1 <= 5. Set i = 1 and swap 9 with 1: [3, 1, 7, 9, 8, 2, 5].

  5. At j = 4, 8 > 5, so nothing moves.

  6. At j = 5, 2 <= 5. Set i = 2 and swap 7 with 2: [3, 1, 2, 9, 8, 7, 5].

  7. Swap A[3] = 9 with pivot A[6] = 5: [3, 1, 2, 5, 8, 7, 9]. Return q = 3.

Now partition [3, 1, 2] around 2. Swap 3 with 1, producing [1, 3, 2, 5, 8, 7, 9], then place the pivot to get [1, 2, 3, 5, 8, 7, 9].

On the right, partition [8, 7, 9] around 9; it returns at index 6 without changing the array. Partition [8, 7] around 7 to finish at [1, 2, 3, 5, 7, 8, 9].

The first partition makes six key-to-pivot comparisons. The complete trace makes 6 + 2 + 2 + 1 = 11. Recursive base-case checks are not key comparisons.

Lomuto trace keeps [8,7,9] unchanged around pivot 9, then partitions [8,7] to reach the sorted array.

Pivot choice determines the recursion shape

For roughly balanced partitions, T(n) = 2T(n/2) + Θ(n). About log n levels each perform Θ(n) work, giving Θ(n log n). See Time Complexity and Asymptotic Notation for the recurrence pattern.

For sorted [1, 2, 3, 4, 5] with last-element Lomuto, pivot 5 leaves sizes 4 and 0, then pivot 4 leaves 3 and 0. The chain is 5 -> 4 -> 3 -> 2 -> 1, with 4 + 3 + 2 + 1 = 10 comparisons. Generally, T(n) = T(n-1) + Θ(n) and the comparison sum is n(n-1)/2, so the worst case is Θ(n^2).

A random pivot gives expected Θ(n log n) time, though its worst case remains Θ(n^2). Median-of-three may improve splits but cannot guarantee balance.

Two-panel recursion comparison shows a sorted five-value worst-case chain with 10 comparisons and Theta(n^2), alongside a balanced pivot-3 tree with Theta(n log n) total work.

Space, stability and duplicate keys

Lomuto and Hoare need O(1) extra array space, but recursion also counts. Ordinary Quick Sort has expected O(log n) and worst-case O(n) stack depth. Recurse on the smaller side and loop over the larger to limit stack use to O(log n), though time can still reach Θ(n^2).

Standard in-place Quick Sort is not stable. With records [2a, 2b, 1] and last-element Lomuto, the pivot swap produces [1, 2b, 2a], reversing the equal-key records.

Duplicates can damage balance. On [4a, 4b, 4c, 4d], Lomuto's <= test sends each pivot to the right end. The count is 3 + 2 + 1 = 6, generalising to Θ(n^2). Three-way partitioning helps on duplicate-heavy data.

How GATE-style questions test Quick Sort

GATE-style questions commonly ask for a partition state, a comparison count, or a recurrence analysis.

State question: Using last-element Lomuto on [9, 3, 7, 1, 8, 2, 5], what array and pivot index result after one partition?

Answer: [3, 1, 2, 5, 8, 7, 9], with q = 3. Hoare may produce a different boundary and state.

Count question: For sorted [1, 2, 3, 4, 5, 6] with last-element Lomuto pivots, how many key comparisons occur?

The total is 5 + 4 + 3 + 2 + 1 = 15; maximally unbalanced recursion takes Θ(n^2).

Recurrence question: Every partition creates subproblems of sizes n/4 and 3n/4 - 1, plus Θ(n) partition work. What is the total time?

The larger branch shrinks by a constant factor, so depth is logarithmic. Linear work per level gives Θ(n log n).

For extra practice, change the pivot rule or the input order and predict which result can change: the partition state, the returned boundary, the comparison count, or all three. Identify the partition rule before calculating any state or count.

Common Quick Sort traps and corrections

  • Trap: Every element left of a Lomuto pivot is smaller. Correction: With <=, left-side values are less than or equal to the pivot.

  • Trap: Hoare's returned boundary is Lomuto's final pivot index. Correction: Lomuto recurses on [low, q-1] and [q+1, high]; this Hoare version uses [low, j] and [j+1, high].

  • Trap: Quick Sort is always Θ(n log n). Correction: Expected or average time is Θ(n log n), while worst-case time is Θ(n^2).

  • Trap: In-place means O(1) total space, or randomisation removes the worst case. Correction: Count the stack; randomisation changes the expectation, not the worst-case bound.

  • Trap: One partition produces fully sorted sides. Correction: Under Lomuto, only the pivot is fixed; each side still needs recursive sorting.

Quick Sort summary

Name the partition scheme, preserve its invariant, use its recursion ranges, and derive time from split balance. Count stack space separately. Here pivot 5 reaches index 3, and full recursion produces [1, 2, 3, 5, 7, 8, 9] in eleven comparisons.

Trace [6, 2, 8, 3, 7, 4] through one last-element Lomuto partition. Check: [2, 3, 4, 6, 7, 8], pivot index 2, five comparisons. Continue with GATE Guidance by Sanchit Sir and the GATE Test Series.