Sorting & Searching
Subject: Coding
Chapter: 3.6 Sorting & Searching
Time limit: 30 minutes
Total marks: 40
Answer all questions. Show brief working where derivations are required.
Q1. (3 marks) State the worst-case, best-case, and average-case time complexities of insertion sort. Explain in one sentence why insertion sort "wins" on nearly-sorted input.
Q2. (4 marks) Trace bubble sort on the array , sorting in ascending order. Write the array after each complete pass.
Q3. (5 marks) Using merge sort, show the recursive splitting and merging steps for the array . Give the final sorted array.
Q4. (5 marks) Given the array , perform one pass of Lomuto partition using the last element as pivot. Show the array after partitioning and give the final index of the pivot.
Q5. (4 marks) For quick sort, state the best-case, average-case, and worst-case time complexities, and give one input pattern that triggers the worst case when the pivot is always the last element.
Q6. (5 marks) Sort the array using counting sort with . Show the count array and the cumulative-count array.
Q7. (4 marks) State the Ω(n log n) lower bound for comparison-based sorting. In one or two sentences, explain how the decision-tree argument gives the bound .
Q8. (5 marks) Perform binary search for the target value in the sorted array
List each mid index examined (0-indexed) and state whether the search succeeds and at what index.
Q9. (3 marks) For each property below, name one sorting algorithm from the chapter that has it: (a) stable and ; (b) in-place and ; (c) non-comparison sort.
Q10. (2 marks) State the time complexity of LSD radix sort on integers of digits each with base , and state why it is a linear-time sort when and are constants.
Answer keyMark scheme & solutions
Q1. (3 marks)
- Worst case: (reverse-sorted). (1)
- Best case: (already sorted). (1)
- Average case: . Insertion sort wins on nearly-sorted data because each element shifts only a few positions; work is where = number of inversions, so it is near-linear when is small. (1)
Q2. (4 marks) Bubble sort passes on :
- Pass 1: (1)
- Pass 2: (1)
- Pass 3: (no swaps) (1)
- Correct sorted result and stopping recognized. (1)
Q3. (5 marks) Split: (1) Down to singletons, then merge:
- (1)
- (1)
- Merge and (1)
- Final: (1)
Q4. (5 marks) Lomuto, pivot (last element), array . Scan with ; increment and swap when :
- : , skip. : , , swap→ (1)
- : , , swap→ (1)
- : skip; : skip; : skip (1)
- : , , swap→ (1)
- Final swap pivot to : , pivot index = 3. (1)
Q5. (4 marks)
- Best: (balanced partitions). (1)
- Average: . (1)
- Worst: . (1)
- Worst-case input with last-element pivot: an already-sorted (or reverse-sorted) array, e.g. . (1)
Q6. (5 marks) Counting sort of , indices .
- Count array (index→count): (2) (value 1→1, 2→2, 3→2, 4→1, 8→1)
- Cumulative counts: (2)
- Output: (1)
Q7. (4 marks)
- Statement: any comparison-based sort requires comparisons in the worst case. (2)
- Argument: a decision tree sorting elements must have at least leaves (one per permutation). A binary tree of height has leaves, so , giving by Stirling. (2)
Q8. (5 marks) Array (0-indexed length 10), target 23.
- lo=0, hi=9, mid=4 → , go right. (1)
- lo=5, hi=9, mid=7 → , go left. (1)
- lo=5, hi=6, mid=5 → = target. (2)
- Success at index 5. Mids examined: 4, 7, 5. (1)
Q9. (3 marks)
- (a) Merge sort. (1)
- (b) Heap sort. (1)
- (c) Counting sort (radix or bucket also acceptable). (1)
Q10. (2 marks)
- Complexity: . (1)
- Linear because with constant and it reduces to ; it is non-comparison, sorting digit-by-digit with stable counting sort per digit. (1)
[
{"claim":"Merge sort of [38,27,43,3,9,82,10] gives sorted array",
"code":"a=[38,27,43,3,9,82,10]; result = sorted(a)==[3,9,10,27,38,43,82]"},
{"claim":"Counting sort output of [4,2,2,8,3,3,1] is correct",
"code":"a=[4,2,2,8,3,3,1]; result = sorted(a)==[1,2,2,3,3,4,8]"},
{"claim":"Lomuto partition with pivot 4 places pivot at index 3",
"code":"a=[7,2,1,6,8,5,3,4]; piv=a[-1]; i=-1\nfor j in range(len(a)-1):\n if a[j]<=piv:\n i+=1; a[i],a[j]=a[j],a[i]\na[i+1],a[-1]=a[-1],a[i+1]\nresult = (i+1==3 and a==[2,1,3,4,8,5,7,6])"},
{"claim":"Binary search finds 23 at index 5",
"code":"a=[2,5,8,12,16,23,38,56,72,91]; lo,hi,idx=0,9,-1\nwhile lo<=hi:\n m=(lo+hi)//2\n if a[m]==23: idx=m; break\n elif a[m]<23: lo=m+1\n else: hi=m-1\nresult = idx==5"}
]