This preview shows pages 1–2. Sign up to view the full content.
;; Kathi Fisler
;; insertion sort : code and notes
;; January 19, 2003
#
Task: write a function sort that consumes a list of numbers and sorts
it into increasing order.
Don't think about which algorithm to use.
Just follow the template and see where it leads you.
If you follow the template, you get the following code
;; sort : listofnumber > listofnumber[sorted]
;; sort list of numbers into increasing order
(define (sort alon)
(cond [(empty? alon) .
..]
[(cons? alon)
... (first alon)
... (sort (rest alon))]))
How do you work from here to a solution?
The examples show you how to
complete the empty case.
When you have to fill in the holes in a more
complicated case, always ask yourself two questions:
1. What do the pieces give me? (what type, and what do they mean)
2. How do I combine the pieces into the answer I want?
For this problem, (first alon) gives you a number, and (sort (rest alon))
"sorts the rest of the list into increasing order" (quotes because I read
this off the purpose statement, replacing "list" with "rest of list").
How
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '07
 Fisler
 Insertion Sort

Click to edit the document details