(Latest Revision: 09/19/2005)

Merge Sort




If you do a merge sort of 2k things 'from the bottom up' it goes
like this:

You are merging                         The step requires

2k-1 pairs of 20=1-element lists
                                     2k moves and 2k-1 compares
2k-2 pairs of 21=2-element lists
                                     2k moves and from 2*2k-2 to 3*2k-2 compares
2k-3 pairs of 22=4-element lists
                                     2k moves and from 4*2k-3 to 7*2k-3 compares
2k-4 pairs of 23=8-element lists
                                     2k moves and from 8*2k-4 to 15*2k-4 compares
.
.
.
                                     2k moves and from 2j*2k-j-1 to (2j-1)*2k-j compares
2k-j-1 pairs of 2j-element lists
                                     2k moves and from 2j*2k-j-1 to (2j+1-1)*2k-(j+1) compares
2k-j-2 pairs of 2j+1-element lists
.
.
.
20=1pair of 2k-1-element lists
                                     2k moves and from 2k-1*20 to (2k-1)*20 compares
one 2k-element lists

Thus the total number of moves required by the sort is k*2k =
(logN)*N, where N=2k is the size of the list.

The total number of compares required could be as little as k*2k-1.
That is the sum of the lower bounds on each "phase".  

The maximum number of compares is 
Σ(2j-1)2k-j, where the index j ranges from 1 to k.

That is the same as Σ2k - Σ2k-j, (in both sums: j=1 to k);

which is the same as k*2k - Σ2i, (i=0 to k-1)

which is the same as k*2k - (2k-1) = (k-1)*2k + 1.

It's pretty easy to see that both the min and max numbers of compares are O(N*logN).

Example: Imagine we are merge-sorting a list of 8 objects.  The max comparison "tree" looks like this:

slot #     1   2   3   4   5   6   7   8
slots    |   |   |   |   |   |   |   |   |
             1       1       1       1
                 3               3
                         7

The figure denotes that we could require 1 compare for each of the initial 4
pairs of 1-element lists, 3 compares for each of the subsequent 2 pairs of
2-element lists, and finally 7 compares for the final pair of 4-element lists.
This would be a total of 4+6+7=17 compares.  The number 17 is also given by
the formula (k-1)*2k + 1, where k=3: (2)*23 + 1 =
2*8+1=17.