Model Question Paper (as circulated)Part A · Q83 marksCO4Module 4
Question 8
Apply the Quick Sort algorithm (using the last element as the pivot) to sort the following list of numbers: [8,4,7,3,10]
Topics: Quick sort
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- Last element pivot with Lomuto partition (CLRS).
Pivot 10 is the largest value, so the first partition moves nothing and 10 stays at index 4. The left part [8, 4, 7, 3] is then partitioned around 3, and so on.
| Input | 8 4 7 3 10 |
|---|---|
| After partition 1 (pivot 10) | 8 4 7 3 10 |
| After partition 2 (pivot 3) | 3 4 7 8 10 |
| After partition 3 (pivot 8) | 3 4 7 8 10 |
| After partition 4 (pivot 7) | 3 4 7 8 10 |
Totals: comparisons = 10, swaps = 1, partitions = 4
How to write it in the exam
Final sorted list: [3, 4, 7, 8, 10]. Show each partition with its pivot (see table above).
How this answer was verified
- tests/pyq-sorting.test.ts
- quick_sort_last_pivot.c — exact output tested
- tests/step-algorithms.test.ts — Lomuto Step algorithm matches the visualizer after every partition