[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]

- Exercises
- Exercise 0: Preparation
- Exercise 1: Comparing Insertion Sorts
- Exercise 2: Testing Insert
- Exercise 3: Inserting strings
- Exercise 4: Generalizing Insertion
- Exercise 5: Displaying Insertion Sort
- Exercise 6: Checking Potential Problems
- Exercise 7: Generalizing Insertion Sort
- Exercise 8: Inserting into Vectors
- Exercise 9: Testing Insertion Sort

Copy the code from the accompanying reading into DrScheme.

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?

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?

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")

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.

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.

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.

Write a procedure,
`(insertion-sort `

.
that generalizes the *list* *less-than-or-equal?*)`insertion-sort-numbers`

procedure.

Write the generalized `insert!`

procedure that inserts a
value into a sorted vector that has "space" at a designated position.

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.)

[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]

**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.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/sorting.html

Source text last modified Fri Nov 17 22:06:20 2000.

This page generated on Fri Nov 17 22:09:27 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu