Sorting by insertion

Course links

Exercise 1

Test the first version of the insert procedure by using it to insert a real number

Exercise 2

What happens if ls is not in ascending order when insert is invoked?

Exercise 3

Modify the first version of the insert procedure so that it inserts a string into a list of strings that are in alphabetical order:

> (modified-insert "dog" (list "ape" "bear" "cat" "emu" "frog"))
("ape" "bear" "cat" "dog" "emu" "frog")

Exercise 4

Test the first version of the insertion-sort procedure on some potentially troublesome arguments: an empty list, a list containing only one element, a list containing all equal values, and a list in which the elements are originally in descending numerical order.

Exercise 5

Use the generalized insertion-sort procedure to sort the clean-sample-data list from the project on summarizing poll responses by the respondents' ID numbers.

(Hint: begin by writing an appropriate ordering predicate -- a procedure that takes two strings as arguments and determines whether the numeral in the first six positions of one string is less than or equal to the numeral in the first six positions of the other string.)

Exercise 6

Create and name a vector containing the strings "bear", "emu", "frog", "ape", "dog", and "cat".

Rearrange the elements of the vector into alphabetical order by means of an appropriate call to insertion-sort!. (Note that the sorting occurs as a side effect of this call -- the value of the do-expression in the body of insertion-sort! is unspecified -- so to confirm that the sorting procedure worked you'll have to inspect the vector again afterwards.)