merge -> merge-sorted-lists

svn: r2576

original commit: 6e34508a02e0984b88b02b7549f6e7d628a02509
This commit is contained in:
Eli Barzilay 2006-04-02 16:09:55 +00:00
parent 0dddf931d8
commit 45981b456d

View File

@ -44,7 +44,7 @@
merge!)
;; used by sort-internal, but can be useful by itself
(define (merge! a b less?)
(define (merge-sorted-lists! a b less?)
(define (loop r a b r-a?) ; r-a? for optimization -- is r connected to a?
(if (less? (car b) (car a))
(begin (when r-a? (set-cdr! r b))
@ -61,8 +61,8 @@
(if (null? (cdr a)) (set-cdr! a b) (loop a (cdr a) b #t))
a]))
;; a non-destructive version for symmetry with merge!
(define (merge a b less?)
;; a non-destructive version for symmetry with merge-sorted-lists!
(define (merge-sorted-lists a b less?)
(cond [(null? a) b]
[(null? b) a]
[else (let loop ([x (car a)] [a (cdr a)] [y (car b)] [b (cdr b)])
@ -88,7 +88,7 @@
(define (step n)
(cond [(> n 3) (let* (; let* not really needed with mzscheme's l->r eval
[j (quotient n 2)] [a (step j)] [b (step (- n j))])
(merge! a b less?))]
(merge-sorted-lists! a b less?))]
;; the following two cases are just explicit treatment of sublists
;; of length 2 and 3, could remove both (and use the above case for
;; n>1) and it would still work, except a little slower