Quick sort divides by a clever partition and does the work before the recursive calls, so no merging is needed.

The Algorithm

Quick sort picks an element called the pivot and partitions the array so that all elements smaller than or equal to the pivot come before it and all larger ones after it. The pivot is then in its final position. Quick sort is called recursively on the left part and the right part. In the Lomuto scheme the last element is the pivot; an index i marks the end of the "small" region, and a scan j from the left swaps each element not larger than the pivot into that region; finally the pivot is swapped to position i + 1. Partitioning n elements takes Θ(n) comparisons.

Analysis

If the pivot splits the array evenly each time, T(n) = 2T(n/2) + n and the best case is Θ(n log n). If the pivot is always the smallest or largest element, which happens when a naive last-element pivot meets an already sorted array, then one part is empty and T(n) = T(n − 1) + n, giving the worst case Θ(n²), with n(n − 1)/2 comparisons. The average case over all permutations is about 1.39 n log₂ n comparisons, Θ(n log n); even a 10:90 split at every level still gives Θ(n log n). Quick sort is in place apart from the recursion stack, O(log n) on average and O(n) in the worst case, and it is not stable.

Choosing the Pivot

Bad pivots cause bad behaviour. Random pivot selection makes the worst case extremely unlikely whatever the input. Median of three (first, middle, last) avoids the sorted-input trap. In practice quick sort is often faster than merge sort because its inner loop is simple and it works well with the cache, which is why it underlies many library sort functions, usually switching to insertion sort for tiny parts.

Definition: Partition. Rearranging an array around a pivot so that elements not greater than the pivot lie before it and greater elements after it, leaving the pivot in its final sorted position.

Table 2.3: Quick sort cases

Case When Complexity
Best Pivot always the median Θ(n log n)
Average Random input or random pivot Θ(n log n), about 1.39 n log n
Worst Sorted input, last-element pivot Θ(n²)
Space Recursion stack O(log n) average

Example 2.3: One partition

Problem: Partition 10, 80, 30, 90, 40, 50, 70 with the last element 70 as pivot.

Solution:

  1. 10 ≤ 70: stays in the small region. 80 > 70: skip.
  2. 30 ≤ 70: swap with 80 → 10 30 80 90 40 50 70.
  3. 90 > 70: skip. 40 ≤ 70: swap with 80 → 10 30 40 90 80 50 70.
  4. 50 ≤ 70: swap with 90 → 10 30 40 50 80 90 70.
  5. Swap the pivot with the first large element (80) → 10 30 40 50 70 90 80.

Result: Pivot 70 is at index 4; left part 10 30 40 50, right part 90 80.

def partition(a, lo, hi):
    pivot, i = a[hi], lo - 1
    for j in range(lo, hi):
        if a[j] <= pivot:
            i += 1
            a[i], a[j] = a[j], a[i]
    a[i + 1], a[hi] = a[hi], a[i + 1]
    return i + 1
a = [10, 80, 30, 90, 40, 50, 70]
p = partition(a, 0, len(a) - 1)
print("pivot index", p, a)
def comps(n):
    return n * (n - 1) // 2
print("worst case comparisons for n = 10:", comps(10))

Output

pivot index 4 [10, 30, 40, 50, 70, 90, 80]
worst case comparisons for n = 10: 45

Common mistakes

  • Using the first or last element as pivot on sorted data and being surprised by Θ(n²).
  • Forgetting to place the pivot in its final position after partitioning.
  • Claiming quick sort is stable; swaps can reorder equal keys.
  • Quoting the worst case as the typical behaviour.

Key takeaways

  • Quick sort partitions around a pivot and recurses on both sides.
  • Best and average Θ(n log n); worst Θ(n²) on bad pivots.
  • It is in place (O(log n) stack on average) but not stable.
  • Random or median-of-three pivots avoid the worst case.

Practice questions

  1. When does quick sort take Θ(n²) time?
  2. What is the recurrence of the best case?
  3. Is quick sort stable?

Answers to the practice questions

  1. When every partition is maximally unbalanced, such as a sorted array with the last element as pivot.
  2. T(n) = 2T(n/2) + n.
  3. No.

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II