B.Tech S3 (R) (FT/WP) Examination November 2025 (2024 Scheme)QP 06PCCST303112503Part B · Q15a6 marksCO4Module 4
Question 15a
Write algorithm to sort an array using quick sort. Apply quick-sort to sort the numbers 45,33,56,22,76,78,40 and 30.
Topics: Quick sort
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- The question does not fix the pivot rule. We use pivot = first element: i moves right past elements ≤ pivot, j moves left past elements > pivot (algorithm below). With distinct keys this gives exactly the same partitions as the Horowitz & Sahni version (i stops at ≥ pivot, j stops at ≤ pivot).
- If your teacher used the last element as pivot, open the simulator and switch to “last element pivot” — intermediate arrays differ, the final answer does not.
Algorithm
- Step 1:Start.
- Step 2:Read the number of elements n and the n elements into the array A.
- Step 3:Call QuickSort(A, 0, n-1).
- Step 4:QuickSort(A, low, high): if low < high, go to Step 5; otherwise return.
- Step 5:Set pivot = A[low], i = low + 1 and j = high.
- Step 6:Repeat while i <= j:
- While i <= high and A[i] <= pivot, increment i.
- While A[j] > pivot, decrement j.
- If i < j, swap A[i] and A[j].
- Step 7:Swap A[low] and A[j]. The pivot is now in its final position j.
- Step 8:Call QuickSort(A, low, j-1).
- Step 9:Call QuickSort(A, j+1, high).
- Step 10:When the call in Step 3 is finished, display the sorted array A.
- Step 11:Stop.
Test i <= high before A[i] <= pivot. If every element is ≤ the pivot, i moves past the end, and testing A[i] first would read outside the array.
Partition 1 in detail (pivot 45)
- i stops at index 2 (56 ≥ 45); j stops at index 7 (30 ≤ 45). i < j → swap → 45, 33, 30, 22, 76, 78, 40, 56
- i stops at index 4 (76); j stops at index 6 (40). Swap → 45, 33, 30, 22, 40, 78, 76, 56
- i stops at index 5 (78); j stops at index 4 (40). i ≥ j → stop.
- Swap pivot a[0] with a[4] → 40, 33, 30, 22, 45, 78, 76, 56. 45 is now final.
| Input | 45 33 56 22 76 78 40 30 |
|---|---|
| After partition 1 (pivot 45) | 40 33 30 22 45 78 76 56 |
| After partition 2 (pivot 40) | 22 33 30 40 45 78 76 56 |
| After partition 3 (pivot 22) | 22 33 30 40 45 78 76 56 |
| After partition 4 (pivot 33) | 22 30 33 40 45 78 76 56 |
| After partition 5 (pivot 78) | 22 30 33 40 45 56 76 78 |
| After partition 6 (pivot 56) | 22 30 33 40 45 56 76 78 |
Totals: comparisons = 25, swaps = 6, partitions = 6
How to write it in the exam
| Call | Pivot | Array after partition |
|---|---|---|
| QS(0,7) | 45 | 40 33 30 22 [45] 78 76 56 |
| QS(0,3) | 40 | 22 33 30 [40] 45 78 76 56 |
| QS(0,2) | 22 | [22] 33 30 40 45 78 76 56 |
| QS(1,2) | 33 | 22 30 [33] 40 45 78 76 56 |
| QS(5,7) | 78 | 22 30 33 40 45 56 76 [78] |
| QS(5,6) | 56 | 22 30 33 40 45 [56] 76 78 |
Sorted: 22, 30, 33, 40, 45, 56, 76, 78. Time complexity: best/average O(n log n), worst O(n²) (already sorted input with first-element pivot).
How this answer was verified
- Hand trace of partition 1
- tests/pyq-sorting.test.ts
- quick_sort_first_pivot.c — exact output tested
- tests/step-algorithms.test.ts — the Step algorithm gives the same array after every partition as the visualizer (distinct keys)