43C. An Efficient Sorting Algorithm: Merge Sort

We can do much better than insertion sort. Merge Sort is based on the fact that it is easy to combine two linked lists that are already sorted.


Merging sorted lists

Supposes that linked lists A and B are already in nondescending order. merge(A, B) should rearrange the list cells of A and B into a combined list that is in nondescending order, and return a pointer to the first cell in the sorted list. For example:

A = [2, 5, 8, 10]
B = [3, 4, 6, 7]
merge(A, B) = [2, 3, 4, 5, 6, 7, 8, 10]

Merge is easy to write. If either list is empty, the merged list is the other one. If both lists are nonempty, suppose that list A begins with hA and list B begins with hB. Then the combined list begins with the smaller of hA and hB. Remove that from its list and continue with what is left.

  List merge(List A, List B)
  {
    if(A == NULL)
    {
      return B;
    }
    else if(B == NULL)
    {
      return A;
    }
    else if(A->head < B->head)
    {
      A->tail = merge(A->tail, B);
      return A;
    }
    else
    {
      B->tail = merge(A, B->tail);
      return B;
    }
  }

Merge Sort

Empty lists are easy to sort. To sort a nonempty list L:

  1. Break L into halves L1 and L2, where L1 is the first half and L2 is the second half.

  2. Sort lists L1 and L2 using Merge Sort.

  3. Merge sorted lists L1 and L2 together into a single sorted list.

The following diagram illustrates.

We show on the next page that Merge Sort takes time Θ(n log2(n)) in the worst case. That is much better than Θ(n2), as shown in the following brief table. The values of nlog(n) are approximate.

n n2 nlog(n)
100 10,000 700
1000 1,000,000 10,000
10,000 100,000,000 130,000