Copy the code from the accompanying reading into DrScheme.
a. Test both versions of the
insert-number procedure from the reading
by inserting a number
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. Show how to call the generalized
insert procedure using
lists of strings.
b. Show how to call the generalized
insert procedure using
lists of numbers.
insert-number in terms of
a. Add calls to the
to the body of the helper in
insertion-sort-numbers so that it displays the values of
sorted, appropriately labeled, at each
step of the sorting process.
b. Use the revised
procedure to sort the values 7, 6, 12, 4, 10, 8, 5, and 1.
insertion-sort-numbers procedure on some potentially
time, determine how long it takes to insertion
sort lists for 50, 100, 150, and 200 numbers. You should probably
try a few different lists of each size. If you find that these
examples are too quick, use larger lists.
You may want to use the following procedure to generate your lists.
;;; Procedure: ;;; random-list ;;; Parameters: ;;; max, the largest value to be produced ;;; len, an integer ;;; Purpose: ;;; Produces a list of "random" values. ;;; Preconditions: ;;; max > 0 ;;; len >= 0 ;;; Postconditions: ;;; The result list has length length. ;;; Every value in the result list is between 0 and max, inclusive. ;;; The result list is hard to predict. (define random-list (lambda (max len) (if (= len 0) null (cons (random (+ max 1)) (random-list max (- len 1))))))
Document, write, and test a procedure,
(insertion-sort list may-precede?).
that generalizes the
Document, write, and test the generalized
to insert into a sorted vector. Your procedure should take the
Your procedure should shift values as appropriate to make space for the new value. Your procedure should not affect values in the vector after space-pos.
Hint: Work backwards from space-pos.
Create and name a vector containing the strings
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
Fall 2000 [Samuel A. Rebelsky]
Tuesday, 24 April 2001 [Samuel A. Rebelsky]
Wednesday, 25 April 2001 [Samuel A. Rebelsky]
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Thu May 3 23:08:05 2001.
This page may be found at
You may validate this page's HTML.
The source was last modified Wed Apr 25 08:25:41 2001.