Functional Problem Solving (CSC 151 2016S) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Summary: In this lab, we explore a variety of issues related to the insertion sort algorithm.
Make a copy of insertion-sort-lab.rkt, the code for this lab.
Write a new insert-string procedure that inserts
a string into a list of strings that are in alphabetical order:
>(insert-string (list "ape" "bear" "cat" "emu" "frog") "dog")("ape" "bear" "cat" "dog" "emu" "frog")
In case you've forgotten, string<=? and
string-ci<=? are useful predicates for
comparing strings for order.
Your goal in this problem is to follow (and therefore better understand)
the pattern of the insert-number procedure.
Hence, you may not use the generalized insert
procedure in writing insert-string.
Let's see how many times the insert-number method
is called. We'll use the techniques from
the lab on analyzing procedures.
a. Note the definitions of counter-new,
counter-count!, counter-reset!, and
counter-print! already in your definitions pane and
remind yourself how they work if you need to.
b. Define a counter named insert-number-counter.
c. Add the following line to the beginning of the
insert-number procedure:
(counter-count! insert-number-counter)
We can count the number of calls to insert-number for
a particular list as follows:
>(counter-reset! insert-number-counter)>(numbers-insertion-sort list)>(counter-print! insert-number-counter)
d. Determine how many calls to insert-number are
involved in sorting each of the following lists.
i. (iota 5)
ii. (iota 10)
iii. (iota 20)
iv. (iota 40)
v. (reverse (iota 5))
vi. (reverse (iota 10))
vii. (reverse (iota 20))
viii. (reverse (iota 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
list-insertion-sort 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 vector-insert! 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 vector-insert!.
c. Extend vector-insert! 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. Re-create 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 randomly-generated numbers.
>(define nums (vector (random 10) (random 10) (random 10) (random 10) (random 10) (random 10) (random 10) (random 10)))>(vector-insertion-sort! nums _____)
f. Explain, in your own words, how
vector-insertion-sort! works.
a. Write a call to the generalized
list-keyed-insertion-sort to sort the list
("clementine" "starfruit" "apple" "kumquat" "pineapple"
"pomegranate") alphabetically.
b. Review the structure of drawing (a
list of named objects). Then write a call to the generalized
list-keyed-insertion-sort
to sort drawing by object name.
c. Write a call to the generalized list-insertion-sort
to sort drawing alphabetically by color name.
d. Write a call to the generalized
list-keyed-insertion-sort to sort drawing by
object width.
Write a procedure, ( that sorts a vector of compound objects by key.
vector-keyed-insertion-sort
vec get-key may-precede?)
While it is possible to write this procedure by converting the vector to
a list, using list-keyed-insertion-sort!, and then
converting the result back to a vector, you should not use this strategy.
Rather, write this procedure directly. (You may want to use
vector-insertion-sort! as a template.)