a. Update the loudhum
package.
b. Make a copy of insertionsortlab.rkt, the code for this lab.
c. With your partner, review your answers to the self checks from the corresponding reading.
Write a new insertstring
procedure that inserts a string into a list
of strings that are in alphabetical order.
> (insertstring (list "ape" "bear" "cat" "emu" "frog") "dog")`
("ape" "bear" "cat" "dog" "emu" "frog")
In case you’ve forgotten, string<=?
and stringci<=?
are useful
predicates for comparing strings for order.
Your goal in this problem is to follow (and therefore better understand)
the pattern of the insertnumber
procedure. Hence, you may not use
the generalized insert
procedure in writing insertstring
.
Let’s see how many times the insertnumber
method is called. We’ll use
the techniques from the lab on analyzing procedures.
a. Define an counter.
b. Add a line to insertnumber
to count each call.
c. Determine how many calls to insertnumber
are
involved in sorting each of the following lists.
i. (range 5)
ii. (range 10)
iii. (range 20)
iv. (range 40)
v. (reverse (range 5))
vi. (reverse (range 10))
vii. (reverse (range 20))
viii. (reverse (range 40))
e. Explain, to the best of your ability, what the numbers you got say about the number of function calls the insertion sort algorithm makes. Your answer should take the length of the list into account.
Write a call to the generalized listinsertionsort
to sort the list
("clementine" "starfruit" "apple" "kumquat" "pineapple" "pomegranate")
alphabetically.
insert!
a. Add the following definition to your definitions pane.
(define numbers (vector 1 5 6 7 2 8 0 3))
b. Check that vectorinsert!
works by using it to move the 2 into the
correct place in the first five spaces in numbers
.
Note: Solving this step requires that you understand the parameters to
vectorinsert!
.
c. Extend vectorinsert!
so that it displays the vector and the
position at every step. That is, add calls to display
and newline
in the kernel, before the cond
.
d. Recreate the numbers
vector from step a, and observe what happens
when we insert the 2, then the 8, then the 0, then the 3.
e. Observe the insertion steps in a vector of about eight randomlygenerated numbers.
> (define nums (vector (random 10) (random 10) (random 10) (random 10)
(random 10) (random 10) (random 10) (random 10)))
> (vectorinsertionsort! nums _____)
f. Explain, in your own words, how vectorinsertionsort!
works.
a. You will note that the file for this lab includes a list called
grinnelldirectory
. Review the structure of grinnelldirectory
.
Then write a call to the generalized listkeyedinsertionsort
to
sort by username.
b. Write a call to the generalized listkeyedinsertionsort
to
sort the list ("clementine" "starfruit" "apple" "kumquat" "pineapple"
"pomegranate" "cantelope")
alphabetically. Note, for example, that
the key of "clementine"
is "clementine"
.
c. Write a call to the generalized listkeyedinsertionsort
to sort
alphabetically by last name and first name. Note, for example, that the
key of ("Rebelsky" "Essay" "muser" "4410")
would be "Rebelsky, Essay"
.
Write a procedure, (indexoflargest ints boundary)
, that takes a vector
of integers and a boundary as parameters and finds the index of the
largest integer between 0 (inclusive) and boundary (exclusive).
Write a procedure, (selectionsort ints)
, that takes a vector of
integers as a parameter and sorts the vector by repeatedly finding
the largest remaining element, swapping it to the boundary, and
decreasing the boundary. The following diagram may help.
+++
 unsorted, smaller  sorted, larger 
+++

boundary
Write a procedure, (stringvectorinsertionsort vec)
,
that sorts a vector of strings. You may not call the generalized
vectorinsertionsort
. However, you may use that procedure
(and the corresponding generalized vectorinsert!
) as a template
for your procedure.
Write a procedure, (vectorkeyedinsertionsort vec getkey
mayprecede?)
, that sorts a vector of lists by key.
While it is possible to write this procedure by converting the vector
to a list, using listkeyedinsertionsort!
, and then converting the
result back to a vector, you should not use this strategy. Rather, write
this procedure so that it calls vectorinsertionsort!
appropriately.