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]
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:
- Choose a pivot.
- Partition so that smaller elements go left, larger go right; the pivot lands in its final sorted position.
- 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
iright whileA[i] <= 78: 45, 23 pass;A[3]=89 > 78→ stop,i = 3. - Move
jleft whileA[j] > 78:A[7]=33not > 78 → stop,j = 7. i < j→ swapA[3]=89andA[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]=12not > 78 → stop,j = 5. i (6) < j (5)?No → stop. Swap pivotA[0]withA[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
iright whileA[i] <= 12:A[0]=12passes;A[1]=45 > 12→ stop,i = 1.jleft whileA[j] > 12: 65, 33, 23, 45 all > 12;A[0]=12not > 12 → stop,j = 0.i (1) < j (0)?No → swap pivotA[0]withA[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
iright whileA[i] <= 45: 45, 23, 33 pass;A[4]=65 > 45→ stop,i = 4.jleft whileA[j] > 45:A[4]=65 > 45→ j=3;A[3]=33not > 45 → stop,j = 3.i (4) < j (3)?No → swap pivotA[1]withA[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
iright whileA[i] <= 33:A[1]=33passes;A[2]=23 <= 33passes → i = 3 (out of range).jleft whileA[j] > 33:A[2]=23not > 33 → stop,j = 2.i (3) < j (2)?No → swap pivotA[1]withA[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
iright whileA[i] <= 90:A[6]=90passes,A[7]=89passes → i = 8 (out of range).jleft whileA[j] > 90:A[7]=89not > 90 → stop,j = 7.i (8) < j (7)?No → swap pivotA[6]withA[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.
| Case | Time |
|---|---|
| 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.