Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This lab is also available in PDF.
Summary: In this lab, we explore a variety of issues related to the insertion sort algorithm.
Contents:
a. Start DrScheme.
b. Copy the code from the accompanying reading into DrScheme.
a. Test both versions of the insert-number
procedure from
the reading by inserting the number 42
(list 42 42 42)
.
b. What happens if the list is not in ascending order when
insert-number
is invoked?
Write a new insert-string
procedure that inserts a string into a
list of strings that are in alphabetical order:
> (insert-string "dog" (list "ape" "bear" "cat" "emu" "frog")) ("ape" "bear" "cat" "dog" "emu" "frog")
In case you've forgotten, string<=?
and
string-ci<=?
are useful predicates for comparing
strings for order.
You may not use the generalized insert
procedure in
writing this procedure.
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.
c. Redefine insert-string
so that it uses insert
as a helper procedure.
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.
Write a test suite (using unit-test.scm
) to test the
insertion-sort-numbers
procedure on some potentially
troublesome arguments:
(load "/home/rebelsky/Web/Courses/CS151/2007S/Examples/unit-test.ss") (begin-tests!) ... (end-tests!)
a. An empty list
b. A list containing only one element
c. A list containing all equal values
d. A list in which the elements are originally in descending numerical order
Document, write, and test a procedure,
(insertion-sort list may-precede?)
.
that generalizes the insertion-sort-numbers
procedure.
insert!
a. Make a copy of the insert!
procedure from
the reading.
b. Check that it works by using insert!
to put the 2 in
the correct place in the following vector
(define numbers (vector 1 5 6 7 2 8 0 3))
(Note that solving this step requires that you understand the parameters to
insert!
.)
c. Extend insert!
so that it displays the vector and the
position at every step. (Add calls to display
and
newline
in the kernel, before the cond
.)
d. Create the numbers vector from step b, and observe what happens when we insert the 2, then the 8, then the 0, then the 3.
e. Make a copy of the insertion-sort!
procedure from
the reading.
f. Observe the insertion steps in a list of about eight randomly-generated numbers.
(define nums (vector (random 10) (random 10) (random 10) (random 10) (random 10) (random 10) (random 10) (random 10)))
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Labs/insertion-sort.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]
-
[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:20 2007.
The source to the document was last modified on Sun Apr 22 21:38:01 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/insertion-sort.html
.
You may wish to
validate this document's HTML
;
;
http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.