Concept / Given Data
Merge sort follows the divide‑and‑conquer paradigm. It recursively splits an unsorted list into halves until each sub‑list contains a single element (trivially sorted), then merges the sub‑lists to produce a sorted whole. Apply it to the array A=[38,27,43,3,9,82,10].
Step‑by‑Step Trace
- Divide Phase (recursively split):
- [38,27,43,3,9,82,10] → [38,27,43] and [3,9,82,10].
- [38,27,43] → [38] and [27,43].
- [27,43] → [27] and [43].
- [3,9,82,10] → [3,9] and [82,10].
- [3,9] → [3] and [9].
- [82,10] → [82] and [10].
- Conquer (Merge) Phase – combine pairs:
- Merge [27] and [43] → [27,43].
- Merge [38] with [27,43] → compare 38 vs 27 → output 27, then 38 vs 43 → output 38, then remaining 43 → [27,38,43].
- Merge [3] and [9] → [3,9].
- Merge [82] and [10] → [10,82].
- Merge [3,9] with [10,82] → [3,9,10,82].
- Final merge of [27,38,43] and [3,9,10,82]:
- Compare 27 vs 3 → output 3.
- 27 vs 9 → output 9.
- 27 vs 10 → output 10.
- 27 vs 82 → output 27.
- 38 vs 82 → output 38.
- 43 vs 82 → output 43.
- Append remaining 82.
- Sorted array: [3,9,10,27,38,43,82].
- Complexity
- Splitting creates log2n levels.
- Each level merges n elements → O(nlogn) total.
- Space: O(n) auxiliary array for merging.
Result: Merge sort transforms [38,27,43,3,9,82,10] into [3,9,10,27,38,43,82] with Θ(nlogn) time and Θ(n) extra space.
Tip from Examiner: In marks distribution, allocate points for (i) divide explanation (2 marks), (ii) merge logic with example (3 marks), (iii) final sorted result (1 mark), and (iv) complexity analysis (2 marks). Common error – forgetting to copy remaining elements after one sub‑list empties.