This lab is also available in PDF.
Summary: In this laboratory, we consider merge sort, a more efficient technique for sorting lists of values.
a. Make a copy of mergesort.scm, my implementation of merge sort. Scan through the code and make sure that you understand all the procedures.
b. Start DrScheme.
a. Write an expresssion to merge the lists
(1 2 3) and
(1 1.5 2.3).
b. Write an expression to merge two identical lists of numbers. For example, you might merge the lists
(1 2 3 5 8 13 21) and
(1 2 3 5 8 13 21)
c. Write an expression to merge two lists of strings. (You may choose the strings yourself. Each list should have at least three elements.)
d. Assume that we represent names as lists of the form
Write an expression to merge the following two lists
(define mathstats-faculty (list (list "Chamberland" "Marc") (list "French" "Chris") (list "Jager" "Leah") (list "Kornelson" "Keri") (list "Kuiper" "Shonda") (list "Moore" "Emily") (list "Moore" "Tom") (list "Mosley" "Holly") (list "Romano" "David") (list "Shuman" "Karen") (list "Wolf" "Royce"))) (define more-faculty (list (list "Moore" "Ed") (list "Moore" "Jonathan") (list "Moore" "Peter")))
a. What will happen if you call
merge with unsorted
lists as the list parameters?
b. Verify your answer by experimentation.
c. What will happen if you call
merge with sorted lists
of very different lengths as the first two parameters?
d. Verify your answer by experimentation.
split to split:
a. A list of numbers of length 6
b. A list of numbers of length 5
c. A list of strings of length 6
merge-sort on a list you design of fifteen integers.
new-merge-sort on a list you design of ten strings.
c. Uncomment the lines in
new-merge-sort that print out
the current list of lists. Rerun
new-merge-sort on a list
you design of ten strings. Is the output what you expect?
new-merge-sort on a list of twenty integers.
a. Run both versions of merge sort on the empty list.
b. Run both versions of merge sort on a one-element list.
c. Run both versions of merge sort on a list with duplicate elements.
As you've probably noticed, there are two key postconditions of a
procedure that sorts lists: The result is a permutation of the original
list and the result is sorted. We're fortunate that the unit
test framework lets us test permutations (with
Hence, if we wanted to test merge sort in the unit test framework, we
(define some-list ...) (test-permutation! (merge-sort some-list pred?) some-list)
However, we still need a way to make sure that the result is sorted, particularly if the result is very long.
Write a procedure,
(sorted? lst may-precede?) that checks
whether or not
lst is sorted by
> (sorted? (list 1 3 5 7 9) <) #t > (sorted? (list 1 3 5 4 7 9) <) #f > (sorted? (list "alpha" "beta" "gamma") string<?)
Note that we can use that procedure in a test suite for merge sort with
(test! (sorted? (merge-sort some-list may-precede?) may-precede?) #t)
One of my colleagues prefers to define
like the following
(define split (lambda (ls) (let kernel ((rest ls) (left null) (right null)) (if (null? rest) (list left right) (kernel (cdr rest) (cons (car rest) right) left)))))
a. How does this procedure split the list?
b. Why might you prefer one version of split over the other?
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:23 2007.
The source to the document was last modified on Tue Apr 24 10:46:21 2007.
This document may be found at
You may wish to validate this document's HTML ; ;Samuel A. Rebelsky, email@example.com
http://creativecommons.org/licenses/by-nc/2.5/or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.