B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part B · Q15a6 marksCO4Module 4
Question 15a
Write algorithm to sort an array using merge sort. Apply merge sort to sort the numbers 45, 36, 12, 65, 24, 11, 44, 54, 17 and 33.
Topics: Merge sort
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- Top-down merge sort with mid = ⌊(lo + hi)/2⌋ (left half gets the extra element).
Algorithm
- Step 1:Start.
- Step 2:Read the number of elements n and the n elements into the array A.
- Step 3:Call MergeSort(A, 0, n-1).
- Step 4:MergeSort(A, low, high): if low < high, go to Step 5; otherwise return.
- Step 5:Set mid = (low + high) / 2.
- Step 6:Call MergeSort(A, low, mid).
- Step 7:Call MergeSort(A, mid+1, high).
- Step 8:Call Merge(A, low, mid, high).
- Step 9:When the call in Step 3 is finished, display the sorted array A.
- Step 10:Stop.
- Step 1:Start.
- Step 2:Set i = low, j = mid + 1 and k = low.
- Step 3:Repeat Steps 4 and 5 while i <= mid and j <= high.
- Step 4:If A[i] <= A[j], copy A[i] to temp[k] and increment i; otherwise copy A[j] to temp[k] and increment j.
- Step 5:Increment k.
- Step 6:Copy the elements that are left:
- While i <= mid, copy A[i] to temp[k] and increment i and k (rest of the first half).
- While j <= high, copy A[j] to temp[k] and increment j and k (rest of the second half).
- Step 7:For i = low to high, copy temp[i] back to A[i].
- Step 8:Stop.
Splitting
text
[45 36 12 65 24 | 11 44 54 17 33]
[45 36 12 | 65 24] [11 44 54 | 17 33]
[45 36 | 12] [65 | 24] [11 44 | 54] [17 | 33]
[45|36] [12] [65] [24] [11|44] [54] [17] [33]| Input | 45 36 12 65 24 11 44 54 17 33 |
|---|---|
| After merge 1: a[0..1] | 36 45 12 65 24 11 44 54 17 33 |
| After merge 2: a[0..2] | 12 36 45 65 24 11 44 54 17 33 |
| After merge 3: a[3..4] | 12 36 45 24 65 11 44 54 17 33 |
| After merge 4: a[0..4] | 12 24 36 45 65 11 44 54 17 33 |
| After merge 5: a[5..6] | 12 24 36 45 65 11 44 54 17 33 |
| After merge 6: a[5..7] | 12 24 36 45 65 11 44 54 17 33 |
| After merge 7: a[8..9] | 12 24 36 45 65 11 44 54 17 33 |
| After merge 8: a[5..9] | 12 24 36 45 65 11 17 33 44 54 |
| After merge 9: a[0..9] | 11 12 17 24 33 36 44 45 54 65 |
Totals: comparisons = 23, writes = 34, merges = 9
How to write it in the exam
text
Merging (bottom-up view):
[36 45] [12] → [12 36 45]
[24 65] → [12 24 36 45 65]
[11 44] [54] → [11 44 54]
[17 33] → [11 17 33 44 54]
[12 24 36 45 65] + [11 17 33 44 54]
→ [11 12 17 24 33 36 44 45 54 65]Time complexity O(n log n) in all cases; extra space O(n).
How this answer was verified
- tests/pyq-sorting.test.ts
- merge_sort.c — exact output tested
- tests/step-algorithms.test.ts — the Step algorithm gives the same array after every merge as the visualizer