insertion sort

# insertion sort - Kathi Fisler insertion sort code and...

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 : list-of-number -> list-of-number[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.

## This note was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.

### Page1 / 2

insertion sort - Kathi Fisler insertion sort code and...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online