Sorting by merging

Exercise 1

Use a merging procedure constructed by merge to combine the lists (2 3 4 7 8 10 12) and (1 6 11 13 14).

Exercise 2

What happens if a merging procedure constructed by merge is applied to lists that are not already in order?

Exercise 3

What happens if a merging procedure constructed by merge is given two empty lists as arguments? Why?

Exercise 4

What are the values of (split (list 'a 'b 'c 'd 'e 'f 'g))? Figure it out by hand first, then use DrScheme to check your answer. Account for any differences between DrScheme's answer and yours.

Exercise 5

Using merge-sort, sort the strings "blanc", "noir", "rouge", "bleu", "jaune", "vert", "gris", "brun", and "rose" into alphabetical order.

Exercise 6

Develop a Scheme procedure that takes a vector of real numbers and determines whether its elements are arranged in ascending numerical order, returning #t if it is and #f if it is not.

Exercise 7

Sometimes, instead of sorting the elements of a list, we want to sort the entries in an association list so that the keys are arranged in a particular order (without regard to the corresponding values). Let's call a procedure that takes an association list as its argument and returns another association list with the same entries, but arranged so that the keys are in a particular order, an alist-sorter.

Develop a Scheme procedure alist-sort that takes, as its only argument, a binary predicate may-precede? expressing an ordering relation, and returns an alist-sorter that arranges the entries of any given association list so that their keys are ordered by may-precede?.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/labs/sorting-by-merging.xhtml

Validated as XHTML 1.1 by the World Wide Web Consortium Cascading Style Sheet validated by the World Wide Web Consortium

created November 25, 1997
last revised October 11, 2001

John David Stone (stone@cs.grinnell.edu)