Contents    Page-10    Prev    Next    Page+10    Index   

Merge Technique

The merge technique can be used to perform a variety of operations on sequences of items (linked lists or arrays) in O(n · log(n)) time:

  1. Sort both sequences: O(n · log(n))

  2. The merging function steps through the sequences one element at a time, looking only at the two front elements and comparing their keys.

  3. The function performs some operation involving one or both front elements, perhaps producing some output.

  4. The function may step down one list, the other list, or both lists.

  5. Examples: merge two sorted lists in order; set intersection, union, or set difference; update a bank account based on a transaction.