Rudy’s OBTF Rudolf Adamkovič

Home / Computer science / Merge sort / Practice implementation


Phase 2: Merge

Merge two sorted lists by

repeatedly taking the smaller of their first elements,

collecting the results into a new sorted list.

(defun my-merge-sort-merge (sorted-list other-sorted-list)
  "Return a sorted list with the elements of SORTED-LIST and
OTHER-SORTED-LIST."
  (cond ((null sorted-list) other-sorted-list)
        ((null other-sorted-list) sorted-list)
        ((<= (car sorted-list)
             (car other-sorted-list))
         (cons (car sorted-list)
               (my-merge-sort-merge (cdr sorted-list)
                                    other-sorted-list)))
        ((> (car sorted-list)
            (car other-sorted-list))
         (cons (car other-sorted-list)
               (my-merge-sort-merge (cdr other-sorted-list)
                                    sorted-list)))))

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.