MCS-208 · Unit 9

Searching and Sorting Techniques

Section 6 of 9

Block 3 · Unit 9Searching and Sorting Techniques6 min · Section 6 of 9

9.62-Way Merge Sort

Covers IGNOU MCS-208 §9.5.4

Merge sort is the other classic divide-and-conquer sort. Instead of splitting carefully and combining trivially (as quick sort does), it splits trivially and does the work when combining: it merges sorted runs into longer sorted runs.

Merging

Merging two sorted lists of total length nn takes O(n)O(n) time: repeatedly move the smaller front element to the output (taking from the left list when the fronts are equal, which keeps the sort stable), then copy what is left.

Bottom-up 2-way merge sort

Treat the input as nn runs of length 1. Merge them in pairs into runs of length 2, then pairs of those into runs of length 4, and so on, until one run remains. Each pass handles every element once (O(n)O(n)), and there are ⌈log⁡2n⌉\lceil \log_2 n \rceil passes, so the time is O(nlog⁡n)O(n \log n) in every case. The merging needs an extra array of nn elements.

Properties

PropertyMerge sort
Time (best, average, worst)O(nlog⁡n)O(n \log n)
Extra space for arraysO(n)O(n) (a second array of nn elements)
StableYes (take from the left run on ties)
Linked listsVery suitable: merging just relinks nodes, no extra array
External sortingThe basis of sorting files on disk (last section)

The top-down version (split in half recursively, sort both halves, merge) does the same work; the bottom-up version avoids recursion.

Key takeaways

  • Merge two sorted runs in O(n)O(n); copy the leftovers.
  • Bottom-up: runs of 1, 2, 4, …; ⌈log⁡2n⌉\lceil \log_2 n \rceil passes.
  • O(nlog⁡n)O(n \log n) in every case; stable; O(n)O(n) extra space for arrays.
  • Ideal for linked lists and for external sorting.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many passes does bottom-up 2-way merge sort need for 20 elements?

Show answer

Answer: B. 5

Run lengths 2, 4, 8, 16, 32: ⌈log⁡220⌉=5\lceil \log_2 20 \rceil = 5 passes.

Question 2 · multiple choice

What is the worst-case time of merge sort?

Show answer

Answer: D. O(nlog⁡n)O(n \log n)

Every pass is O(n)O(n) whatever the input, and there are O(log⁡n)O(\log n) passes. Unlike quick sort, there is no bad input.

Question 3 · multiple choice

What is the main disadvantage of merge sort on arrays?

Show answer

Answer: B. It needs an extra array of nn elements.

Merging into a temporary array costs O(n)O(n) extra space. Merge sort is stable and O(nlog⁡n)O(n \log n).

Question 4 · multiple choice

What does this program print?

C · q9-merge.c
#include <stdio.h>

int main(void)
{
    int a[] = {3, 8, 12}, b[] = {5, 8, 20, 25}, c[7];
    int i = 0, j = 0, k = 0;
    while (i < 3 && j < 4) c[k++] = (a[i] <= b[j]) ? a[i++] : b[j++];
    while (i < 3) c[k++] = a[i++];
    while (j < 4) c[k++] = b[j++];
    for (k = 0; k < 7; k++) printf("%d ", c[k]);
    printf("(i=%d j=%d)\n", i, j);
    return 0;
}
Show answer

Answer: A. 3 5 8 8 12 20 25 (i=3 j=4)

The merge takes the smaller front each time; after a is used up, 20 and 25 are copied from b. All 7 elements appear in order.

Question 5 · multiple choice

Why is merge sort a good choice for sorting a linked list?

Show answer

Answer: D. Merging only relinks nodes, so no extra array is needed, and only sequential access is used.

Merge sort reads runs from front to back, which linked lists do well; quick sort and heap sort need random access.

Question 6 · short answer

Sort 38 27 43 3 9 82 10 with bottom-up 2-way merge sort, showing the runs after each pass.

Show worked answer

Input runs of 1: 38 | 27 | 43 | 3 | 9 | 82 | 10. Pass 1 (runs of 2): 27 38 | 3 43 | 9 82 | 10. Pass 2 (runs of 4): 3 27 38 43 | 9 10 82. Pass 3 (runs of 8): 3 9 10 27 38 43 82. Three passes (⌈log⁡27⌉=3\lceil \log_2 7 \rceil = 3), each O(n)O(n).

↑ ↓ move↵ openTab switch type