Lab: Sorting

Exercises

Exercise 0: Preparation

Copy the code from the accompanying reading into DrScheme.

Exercise 1: Comparing Insertion Sorts

Discuss with your partner (and perhaps a neighboring group) the significant differences between the list-based insertion sort described in the first part of the the reading and the insertion sort we discussed in class (and that appears in the second part of the reading). What is different other than that one deals with lists and the other with vectors?

Exercise 2: Testing Insert

a. Test both versions of the `insert-number` procedure from the reading by inserting a number

• into an empty list;
• into a list of larger numbers, arranged in ascending order;
• into a list of smaller numbers, arranged in ascending order;
• into a mixed list, arranged in ascending order;
• into a list of copies of the number to be inserted.

b. What happens if `ls` is not in ascending order when `insert-number` is invoked?

Exercise 3: Inserting strings

Write a new `string-insert` procedure that inserts a string into a list of strings that are in alphabetical order:

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

Exercise 4: Generalizing Insertion

a. Write a generalized `insert` procedure that also takes a less-than-or-equal-to predicate as a parameter.

b. Show how to call this procedure using lists of strings and numbers.

Exercise 5: Displaying Insertion Sort

a. Add calls to the `display` and `newline` procedures to the body of the helper in `insertion-sort-numbers` so that it displays the values of `unsorted` and `sorted`, appropriately labeled, at each step of the sorting process.

b. Use the revised `insertion-sort-numbers` procedure to sort the values 7, 6, 12, 4, 10, 8, 5, and 1.

Exercise 6: Checking Potential Problems

Test the `insertion-sort-numbers` procedure on some potentially troublesome arguments:

• an empty list,
• a list containing only one element,
• a list containing all equal values,
• a list in which the elements are originally in descending numerical order.

Exercise 7: Generalizing Insertion Sort

Write a procedure, `(insertion-sort list less-than-or-equal?)`. that generalizes the `insertion-sort-numbers` procedure.

Exercise 8: Inserting into Vectors

Write the generalized `insert!` procedure that inserts a value into a sorted vector that has "space" at a designated position.

Exercise 9: Testing Insertion Sort

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. Hence, to confirm that the sorting procedure worked you'll have to inspect the vector again afterwards.)

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.