Contents    Page-10    Prev    Next    Page+10    Index   

Merge

To merge two sorted lists means to combine them into a single list so that the combined list is sorted. We will consider both constructive and destructive versions of merge.

The general idea of a merge is to walk down two sorted lists simultaneously, advancing down one or the other based on comparison of the top values using the sorting function.

In combining two sorted lists with a merge, we walk down both lists, putting the smaller value into the output at each step. Duplicates are retained in a merge.


(merge 'list '(3 7 9) '(1 3 4) '<)

          ->  (1 3 3 4 7 9)

For simplicity, we will define a function merj that assumes a list of comparable objects and an ascending sort.


(merj '(3 7 9) '(1 3 4))   ->  (1 3 3 4 7 9)