Merge algorithm

From Wikipedia, the free encyclopedia

Merge algorithms are a family of algorithms that run sequentially over multiple sorted lists, typically producing more sorted lists as output. This is well-suited for machines with tape drives. Use has declined due to large random access memories, and many applications of merge algorithms have faster alternatives when you have a random-access memory that holds all your data.

The general merge algorithm has a set of pointers p0..n that point to positions in a set of lists L0..n. Initially they point to the first item in each list. The algorithm is as follows:

While any of p0..n still point to data inside of L0..n instead of past the end:

  1. do something with the data items p0..n point to in their respective lists
  2. find out which of those pointers points to the item with the lowest key; advance one of those pointers to the next item in its list

[edit] Pseudocode

A simple non-recursive pseudocode implementation of merge with two lists might be written as follows:

function merge(a, b)
        var list result
        var int i, j := 0
        
        while (i < length(a)) and (j < length(b))
                if a[i] < b[j]
                        add a[i] to result
                       i := i + 1
                else
                        add b[j] to result
                        j := j + 1

        while i < length(a)
                add a[i] to result
                i := i + 1
        while j < length(b)
                add b[j] to result
                j := j + 1
        
        return result

[edit] Analysis

Merge algorithms generally run in time proportional to the sum of the lengths of the lists; merge algorithms that operate on large numbers of lists at once will multiply the sum of the lengths of the lists by the time to figure out which of the pointers points to the lowest item, which can be accomplished with a heap-based priority queue in O(lg n) time, for O(m lg n) time (where n is the number of lists being merged, m is the sum of the lengths of the lists, and lg is log base 2). When merging two lists of length m, there is a lower bound of 2m − 1 comparisons required in the worst case.

The classic merge (the one used in merge sort) outputs the data item with the lowest key at each step; given some sorted lists, it produces a sorted list containing all the elements in any of the input lists, and it does so in time proportional to the sum of the lengths of the input lists.

[edit] Uses

Merge can also be used for a variety of other things:

  • given a set of current account balances and a set of transactions, both sorted by account number, produce the set of new account balances after the transactions are applied; this requires always advancing the "new transactions" pointer in preference to the "account number" pointer when the two have equal keys, and adding all the numbers on either tape with the same account number to produce the new balance.
  • produce a sorted list of records with keys present in all the lists (equijoin); this requires outputting a record whenever the keys of all the p0..n are equal.
  • similarly for finding the largest number on one tape smaller than each number on another tape (e.g. to figure out what tax bracket each person is in).
  • similarly for computing set differences: all the records in one list with no corresponding records in another.
  • insertion/update operations in search engines to produce an inverted index
  • merging plays a central role in Dijkstra's functional programming technique for generating regular numbers

[edit] Very simple SML example

fun merge(a, b) = if a=nil orelse b=nil then a@b
        else if hd(a)<hd(b) then hd(a)::merge(tl(a), b)
        else if hd(a)>hd(b) then hd(b)::merge(a, tl(b))
        else [hd(a), hd(b)]@merge(tl(a), tl(b));

[edit] C

void Merge(float v[], int start, int mid, int end) 
{
        int i, j, k;
        float* tmp = malloc(sizeof(float) * (end - start + 1));
        i = start;
        j = mid;
        k = 0;
        while ((i < mid) && (j <= end))
        {
                if (v[i] <= v[j])
                { 
                        tmp[k++] = v[i];
                        if (v[i] == v[j]) j++;
                        i++;
                }
                else
                        tmp[k++] = v[j++];
        }
        while (i < mid) 
                tmp[k++] = v[i++];
        while (j <= end) 
                tmp[k++] = v[j++];
        for (i = 0; i < (end - start + 1); i++) 
                v[start+i] = tmp[i];
        free(tmp); 
}
  • C++'s STL has the function std::merge which merges two sorted ranges of iterators, and the function std::inplace_merge which merges two consecutive sorted ranges in-place. In addition, the std::list (linked list) class has its own merge method which merges another list into itself. The type of the elements merged must support the < operator, or you must provide it with a custom comparator.

[edit] References

  • Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 158–160 of section 5.2.4: Sorting by Merging. Section 5.3.2: Minimum-Comparison Merging, pp.197–207.
Languages