previous | index | next

Implementing Mergesort

A list can be separated into two only if there are two or more elements, so there are two base cases:

Recall that mergesort uses a divide and conquer strategy.

Suppose we divide the list using one-part and the-other-part, and we merge them with merge:

     (define mergesort
       (lambda (lst)
         (cond ((null? lst) '())       ; first base case
               ((null? (cdr lst)) lst) ; second base case
               (else (merge (mergesort (one-part lst))
                            (mergesort (the-other-part lst)))))))

previous | index | next