This preview shows pages 1–10. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: HigherOrder Functions and Loops c. Kathi Fisler, 20012004 Warm Up: Sorting a List of Numbers Remember quicksort? (quicksort (list 3 9 6 2 1 7)) (quicksort (list 2 1)) (quicksort (list 9 6 7)) 3 [smaller than pivot] [larger than pivot] [pivot] (list 1) (list 7) (quicksort (list 1)) (quicksort empty) 2 (quicksort empty) (quicksort (list 6 7)) 9 (quicksort (list 7)) 6 Answer: (list 1 2 3 6 7 9) Warm Up: Sorting a List of Numbers Lets write quicksort. As usual, start with the template for list[num] ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) Warm Up: Sorting a List of Numbers What do the pieces in the cons? case give us? ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) sorts the rest of the list into increasing order a number Warm Up: Sorting a List of Numbers So, how do we combine them? We need to insert the first element into the sorted rest of the list ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) sorts the rest of the list into increasing order a number But thats insertion sort! Writing quicksort via templates We got insertionsort. What happened? With templates, you write programs according to the natural recursion Insertionsort is the naturally recursive sort Quicksort uses recursion in a different way Moral: some algorithms need different forms of recursion (generative recursion see HTDP). Templates arent a catchall for program design (but they are still very useful for lots of programs) Quicksort: Take 2 The template is fine until the natural recursion, so well take that out and leave the rest intact ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) How did quicksort work? Gather the elts smaller than (first alon); gather those larger; sort; and combine: Quicksort: Take 2 How did quicksort work? Gather the elts smaller than (first alon); gather those larger; sort; and combine: ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (smallerthan (first alon) (rest alon)) (largerthan (first alon) (rest alon)) ])) gather the larger elts gather the smaller elts [well write smallerthan, largerthan later] Quicksort: Take 2 How did quicksort work? Gather the elts smaller than (first alon); gather those larger; sort; and combine: ;; quicksort : list[num] J list[num] ;; sorts a list of nums into increasing order...
View
Full
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.
 Fall '07
 Fisler
 Sort

Click to edit the document details