0 3

BIT201 · 0 · 10 marks

Define sorting problem. Trace quick sort algorithm for the following given list of data and also discuss about its time complexity: 78 45 23 89 65 12 90 33

Define sorting problem. Trace quick sort algorithm for the following given list of data and also discuss about its time complexity: 78 45 23 89 65 12 90 33[10]

Data Structures and AlgorithmsChapter 6: Sorting AlgorithmsQuick sort algorithm and tracingNumerical: quick sort trace

Sorting Problem and Quick Sort

Definition of Sorting Problem

The sorting problem is stated as:

  • Input: A sequence of $n$ numbers $\langle a_1, a_2, \dots, a_n \rangle$.
  • Output: A permutation (rearrangement) $\langle a_1', a_2', \dots, a_n' \rangle$ of the input such that $a_1' \le a_2' \le \dots \le a_n'$.

That is, given an unordered list, arrange its elements in non-decreasing (or non-increasing) order.

Given Data

List to sort:

$$78,\ 45,\ 23,\ 89,\ 65,\ 12,\ 90,\ 33$$

Indices $[0..7]$.

Quick Sort Concept

Quick Sort is a divide-and-conquer algorithm:

  1. Choose a pivot.
  2. Partition so that smaller elements go left, larger go right; the pivot lands in its final sorted position.
  3. Recursively sort the left and right subarrays.

I use the first element as pivot with the standard Lomuto/Hoare-style scan.

Tracing Quick Sort

Pass 1: partition(A, 0, 7), pivot = 78

Array: 78 45 23 89 65 12 90 33

  • Move i right while A[i] <= 78: 45, 23 pass; A[3]=89 > 78 → stop, i = 3.
  • Move j left while A[j] > 78: A[7]=33 not > 78 → stop, j = 7.
  • i < j → swap A[3]=89 and A[7]=33:

78 45 23 33 65 12 90 89

  • Continue i: 33, 65, 12 pass; A[6]=90 > 78 → stop, i = 6.
  • Continue j: A[7]=89 > 78 → j=6; A[6]=90 > 78 → j=5; A[5]=12 not > 78 → stop, j = 5.
  • i (6) < j (5)? No → stop. Swap pivot A[0] with A[5]:

12 45 23 33 65 78 90 89 → pivot 78 fixed at index 5.

Left subarray: indices [0..4] = 12 45 23 33 65 Right subarray: indices [6..7] = 90 89

Pass 2a: partition(A, 0, 4), pivot = 12

12 45 23 33 65

  • i right while A[i] <= 12: A[0]=12 passes; A[1]=45 > 12 → stop, i = 1.
  • j left while A[j] > 12: 65, 33, 23, 45 all > 12; A[0]=12 not > 12 → stop, j = 0.
  • i (1) < j (0)? No → swap pivot A[0] with A[0] (itself).

12 45 23 33 65 78 90 89 → 12 fixed at index 0.

Left subarray [0..-1] empty; right subarray [1..4] = 45 23 33 65.

Pass 2b: partition(A, 1, 4), pivot = 45

45 23 33 65

  • i right while A[i] <= 45: 45, 23, 33 pass; A[4]=65 > 45 → stop, i = 4.
  • j left while A[j] > 45: A[4]=65 > 45 → j=3; A[3]=33 not > 45 → stop, j = 3.
  • i (4) < j (3)? No → swap pivot A[1] with A[3]:

Swap A[1]=45 and A[3]=33:

12 33 23 45 65 78 90 89 → 45 fixed at index 3.

Left subarray [1..2] = 33 23; right subarray [4..4] = 65 (single element, sorted).

Pass 3: partition(A, 1, 2), pivot = 33

33 23

  • i right while A[i] <= 33: A[1]=33 passes; A[2]=23 <= 33 passes → i = 3 (out of range).
  • j left while A[j] > 33: A[2]=23 not > 33 → stop, j = 2.
  • i (3) < j (2)? No → swap pivot A[1] with A[2]:

Swap A[1]=33 and A[2]=23:

12 23 33 45 65 78 90 89 → 33 fixed at index 2.

Left [1..1] = 23 sorted; right [3..2] empty.

Pass 4: partition(A, 6, 7), pivot = 90

90 89

  • i right while A[i] <= 90: A[6]=90 passes, A[7]=89 passes → i = 8 (out of range).
  • j left while A[j] > 90: A[7]=89 not > 90 → stop, j = 7.
  • i (8) < j (7)? No → swap pivot A[6] with A[7]:

Swap A[6]=90 and A[7]=89:

12 23 33 45 65 78 89 90 → 90 fixed at index 7.

Left [6..6] = 89 sorted; right empty.

Final Sorted List

$$\boxed{12,\ 23,\ 33,\ 45,\ 65,\ 78,\ 89,\ 90}$$

Time Complexity

Let $T(n)$ be the running time. Partitioning an array of size $n$ costs $\Theta(n)$.

Best case: Pivot splits the array into two nearly equal halves.

$$T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n)$$

Average case: For random inputs the split is balanced enough that

$$T(n) = \Theta(n \log n)$$

Worst case: Pivot is always the smallest/largest element (e.g. already sorted data with first-element pivot), giving splits of size $0$ and $n-1$:

$$T(n) = T(n-1) + \Theta(n) = \Theta(n^2)$$

Space complexity: $O(\log n)$ average recursion stack, $O(n)$ worst case; it is an in-place sort.

CaseTime
Best$\Theta(n\log n)$
Average$\Theta(n\log n)$
Worst$\Theta(n^2)$

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.