Module 3
Generative recursion
There’s more to programming than the list template?
CS 116: Introduction to Computer Science 2
Daniel G. Brown/Troy Vasiga, University of Waterloo
3.1
Purpose of Module 3
•
Algorithms: generative recursion
•
Standard algorithms for typical dataprocessing problems
•
How to successfully design generative recursive code
•
Readings:
HtDP 25, 26, Intermezzo 5
3.2
1
Algorithms: generative recursion
Less obvious recursion
All of our recursions so far have been “natural”:
•
simple base case
•
straightforward inductive case that works toward that base case.
We called this
structural
recursion.
Some examples of recursion aren’t nearly so “clean”
•
Each run through a procedure may get us one step closer to the goal, but how we get there may
be a lot more uncertain.
•
We may also not know how many steps the algorithm will take.
•
Or, each step might be more complicated in its own right.
3.3
More complex recursion?
Why might we use more complex recursion?
•
to make the program faster,
•
to solve a hard problem where structural recursion isn’t the obvious way to solve it.
We will call this style of recursion
generative
recursion, because we have to think in order to
identify the recursive cases in the development: we must “generate” them.
3.4
2
Sorting algorithms
2.1
Insertion sort
Sorting
Possibly the most basic example of doing this: sorting a list of numbers.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document;; sortlist: (listof num)
→
(listof num)
;; creates a list with all of the same elements as lst,
;; but in sorted order from smallest to largest
;; Example:
;; (sortlist (list 1 4 3 2))
⇒
(list 1 2 3 4)
An algorithm from 115 used structural recursion: To sort a list:
•
Sort all but the ﬁrst element of the list.
•
Then, insert the ﬁrst element into its proper place in the sorted remainder of the list.
(
define
(
sortlist lst
)
(
cond
[(
empty
?
lst
)
empty
]
[
else
(
insert
(
first
lst
)
(
sortlist
(
rest
lst
)))]))
Do you remember how to write the function
insert
?
3.5
Finishing off insertion sort
;; insert: num (listof num)
→
(listof num)
;; insert takes item and a list of sorted numbers and inserts
;; item into the correct position so that the produced list
;; is in sorted order
(
define
(
insert item sortedlist
)
(
cond
[(
empty
?
sortedlist
) (
list
item
)]
[(<
item
(
first
sortedlist
))
(
cons
item sortedlist
)]
[
else
(
cons
(
first
sortedlist
)
(
insert item
(
rest
sortedlist
)))]))
•
Both
sortlist
and its helper function,
insert
, use natural recursion.
•
It’s especially clean in the case of
sortlist
.
•
You could actually write them both using
foldr
.
This algorithm is called
insertion sort
.
3.6
2.2
Selection sort
Selection sort
Another way of sorting a list:
•
Remove the smallest element from the list
•
Sort what’s left
•
Put the smallest element ﬁrst.
This suggests code like this:
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '09
 T.VASIGA
 Computer Science, Algorithms, Recursion, Insertion Sort, lst, builtin function

Click to edit the document details