insertion sort

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

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online