MCS-208 · Unit 9

Searching and Sorting Techniques

Section 5 of 9

Block 3 · Unit 9Searching and Sorting Techniques12 min · Section 5 of 9

9.5Quick Sort

Covers IGNOU MCS-208 §9.5.3

Quick sort, invented by C. A. R. Hoare around 1960, is the most widely used internal sorting method. It uses divide and conquer: one element, the pivot, is put in its final place, with smaller elements on its left and larger ones on its right, and the two sides are then sorted the same way.

Partitioning

To sort A[m..n]A[m..n] with pivot k=A[m]k = A[m]:

  1. Set i=mi = m and j=n+1j = n + 1.
  2. Move ii right until A[i]≥kA[i] \ge k (or ii passes nn).
  3. Move jj left until A[j]≤kA[j] \le k.
  4. If i<ji < j, swap A[i]A[i] and A[j]A[j] and go back to step 2; otherwise the scans have crossed.
  5. Swap A[m]A[m] (the pivot) with A[j]A[j]. Now the pivot is at jj, everything left of it is ≤k\le k and everything right of it is ≥k\ge k.
  6. Sort A[m..j−1]A[m..j-1] and A[j+1..n]A[j+1..n] recursively.

Running time

CaseWhenTime
Best / averagePivots split the array roughly in halfO(nlog⁡n)O(n \log n) comparisons
WorstPivot is always the smallest or largest (e.g. sorted input with first-element pivot)n(n−1)2=O(n2)\frac{n(n-1)}{2} = O(n^2)
Extra spaceRecursion stackO(log⁡n)O(\log n) on average, O(n)O(n) worst

Quick sort is not stable. Practical improvements: choose the pivot as the median of three (first, middle, last) or at random, which makes the worst case very unlikely; switch to insertion sort for small sub-arrays (about 10–20 elements); recurse on the smaller part first to keep the stack O(log⁡n)O(\log n).

Key takeaways

  • Divide and conquer: partition around a pivot, then sort both sides recursively.
  • Two scans move towards each other and swap out-of-place pairs; the pivot ends at jj.
  • Bound the left scan (or use a sentinel) so it cannot leave the array.
  • Average O(nlog⁡n)O(n \log n), worst O(n2)O(n^2); not stable; median-of-three or random pivots help.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which programming technique does quick sort use?

Show answer

Answer: A. Divide and conquer

It divides the array around a pivot and solves the two parts recursively (Check Your Progress 3, Q2).

Question 2 · multiple choice

After one partition with pivot 40 on 40 20 70 10 50 30, which statement must be true?

Show answer

Answer: B. 40 is in its final sorted position, with 20, 10, 30 to its left in some order.

Partitioning only guarantees the pivot’s position and the split; each side may still be unsorted.

Question 3 · multiple choice

When does quick sort with the first element as pivot take O(n2)O(n^2) time?

Show answer

Answer: C. When the input is already sorted (or reverse sorted)

Each partition then removes only the pivot, giving sub-arrays of sizes n−1,n−2,…n-1, n-2, \dots: about n2/2n^2/2 comparisons, as the program shows.

Question 4 · multiple choice

What does this program print?

C · q9-partition.c
#include <stdio.h>

int main(void)
{
    int A[] = {30, 50, 10, 70, 20};
    int m = 0, n = 4, k = A[m], i = m, j = n + 1, t;
    while (1) {
        do i++; while (i <= n && A[i] < k);
        do j--; while (A[j] > k);
        if (i >= j) break;
        t = A[i]; A[i] = A[j]; A[j] = t;
    }
    t = A[m]; A[m] = A[j]; A[j] = t;
    printf("j=%d:", j);
    for (int x = 0; x < 5; x++) printf(" %d", A[x]);
    printf("\n");
    return 0;
}
Show answer

Answer: B. j=2: 10 20 30 70 50

i stops at 50, j at 20: swap → 30 20 10 70 50. Next i stops at 70 (index 3), j at 10 (index 2): crossed. Swapping the pivot with A[2] gives 10 20 30 70 50; the right side is not sorted yet.

Question 5 · multiple choice

What is the average-case time of quick sort?

Show answer

Answer: D. O(nlog⁡n)O(n \log n)

On average the pivots split the array reasonably evenly, giving O(log⁡n)O(\log n) levels of O(n)O(n) work.

Question 6 · short answer

Write a C function for quick sort using the first element as pivot, and state its best, average and worst-case complexity.

Show worked answer

void quicksort(int A[], int m, int n) { if (m >= n) return; int k = A[m], i = m, j = n + 1, t; while (1) { do i++; while (i <= n && A[i] < k); do j--; while (A[j] > k); if (i >= j) break; t = A[i]; A[i] = A[j]; A[j] = t; } t = A[m]; A[m] = A[j]; A[j] = t; /* pivot to its place */ quicksort(A, m, j - 1); quicksort(A, j + 1, n); } Best and average case O(nlog⁡n)O(n \log n); worst case O(n2)O(n^2) (e.g. sorted input with this pivot choice).

↑ ↓ move↵ openTab switch type