Level 2 — RecallSorting & Searching

Sorting & Searching

30 minutes40 marksprintable — key stays hidden on paper

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 [5,1,4,2,8][5, 1, 4, 2, 8], 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 [38,27,43,3,9,82,10][38, 27, 43, 3, 9, 82, 10]. Give the final sorted array.

Q4. (5 marks) Given the array [7,2,1,6,8,5,3,4][7, 2, 1, 6, 8, 5, 3, 4], 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 [4,2,2,8,3,3,1][4, 2, 2, 8, 3, 3, 1] using counting sort with k=8k = 8. 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 Ω(nlogn)\Omega(n \log n).

Q8. (5 marks) Perform binary search for the target value 2323 in the sorted array [2,5,8,12,16,23,38,56,72,91].[2, 5, 8, 12, 16, 23, 38, 56, 72, 91]. 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 O(nlogn)O(n \log n); (b) in-place and O(nlogn)O(n \log n); (c) O(n+k)O(n + k) non-comparison sort.

Q10. (2 marks) State the time complexity of LSD radix sort on nn integers of dd digits each with base kk, and state why it is a linear-time sort when dd and kk are constants.

Answer keyMark scheme & solutions

Q1. (3 marks)

  • Worst case: O(n2)O(n^2) (reverse-sorted). (1)
  • Best case: O(n)O(n) (already sorted). (1)
  • Average case: O(n2)O(n^2). Insertion sort wins on nearly-sorted data because each element shifts only a few positions; work is O(n+I)O(n + I) where II = number of inversions, so it is near-linear when II is small. (1)

Q2. (4 marks) Bubble sort passes on [5,1,4,2,8][5,1,4,2,8]:

  • Pass 1: [1,4,2,5,8][1,4,2,5,8] (1)
  • Pass 2: [1,2,4,5,8][1,2,4,5,8] (1)
  • Pass 3: [1,2,4,5,8][1,2,4,5,8] (no swaps) (1)
  • Correct sorted result and stopping recognized. (1)

Q3. (5 marks) Split: [38,27,43,3,9,82,10][38,27,43,3][9,82,10][38,27,43,3,9,82,10] \to [38,27,43,3]\,|\,[9,82,10] (1) Down to singletons, then merge:

  • [27,38],[3,43][3,27,38,43][27,38], [3,43] \to [3,27,38,43] (1)
  • [9,82],[10][9,10,82][9,82],[10] \to [9,10,82] (1)
  • Merge [3,27,38,43][3,27,38,43] and [9,10,82][9,10,82] (1)
  • Final: [3,9,10,27,38,43,82][3, 9, 10, 27, 38, 43, 82] (1)

Q4. (5 marks) Lomuto, pivot =4= 4 (last element), array [7,2,1,6,8,5,3,4][7,2,1,6,8,5,3,4]. Scan with i=1i = -1; increment ii and swap when a[j]4a[j] \le 4:

  • j=0j=0: 7>47>4, skip. j=1j=1: 242\le4, i=0i=0, swap→[2,7,1,6,8,5,3,4][2,7,1,6,8,5,3,4] (1)
  • j=2j=2: 141\le4, i=1i=1, swap→[2,1,7,6,8,5,3,4][2,1,7,6,8,5,3,4] (1)
  • j=3j=3: 6>46>4 skip; j=4j=4: 8>48>4 skip; j=5j=5: 5>45>4 skip (1)
  • j=6j=6: 343\le4, i=2i=2, swap→[2,1,3,6,8,5,7,4][2,1,3,6,8,5,7,4] (1)
  • Final swap pivot to i+1=3i+1=3: [2,1,3,4,8,5,7,6][2,1,3,4,8,5,7,6], pivot index = 3. (1)

Q5. (4 marks)

  • Best: O(nlogn)O(n \log n) (balanced partitions). (1)
  • Average: O(nlogn)O(n \log n). (1)
  • Worst: O(n2)O(n^2). (1)
  • Worst-case input with last-element pivot: an already-sorted (or reverse-sorted) array, e.g. [1,2,3,4,5][1,2,3,4,5]. (1)

Q6. (5 marks) Counting sort of [4,2,2,8,3,3,1][4,2,2,8,3,3,1], indices 0..80..8.

  • Count array (index→count): [0,1,2,2,1,0,0,0,1][0,1,2,2,1,0,0,0,1] (2) (value 1→1, 2→2, 3→2, 4→1, 8→1)
  • Cumulative counts: [0,1,3,5,6,6,6,6,7][0,1,3,5,6,6,6,6,7] (2)
  • Output: [1,2,2,3,3,4,8][1, 2, 2, 3, 3, 4, 8] (1)

Q7. (4 marks)

  • Statement: any comparison-based sort requires Ω(nlogn)\Omega(n \log n) comparisons in the worst case. (2)
  • Argument: a decision tree sorting nn elements must have at least n!n! leaves (one per permutation). A binary tree of height hh has 2h\le 2^h leaves, so 2hn!2^h \ge n!, giving hlog2(n!)=Θ(nlogn)h \ge \log_2(n!) = \Theta(n\log n) by Stirling. (2)

Q8. (5 marks) Array (0-indexed length 10), target 23.

  • lo=0, hi=9, mid=4 → a[4]=16<23a[4]=16 < 23, go right. (1)
  • lo=5, hi=9, mid=7 → a[7]=56>23a[7]=56 > 23, go left. (1)
  • lo=5, hi=6, mid=5 → a[5]=23a[5]=23 = 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: O(d(n+k))O(d(n+k)). (1)
  • Linear because with constant dd and kk it reduces to O(n)O(n); 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"}
]