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 ;; quicksort : list[num] list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) Lets write quicksort. As usual, start with the template for list[num] Warm Up: Sorting a List of Numbers ;; quicksort : list[num] list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) What do the pieces in the cons? case give us? sorts the rest of the list into increasing order a number Warm Up: Sorting a List of Numbers ;; quicksort : list[num] list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) So, how do we combine them? We need to insert the first element into the sorted rest of the list 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 ;; quicksort : list[num] list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (first alon) (quicksort (rest alon)) ])) The template is fine until the natural recursion, so well take that out and leave the rest intact How did quicksort work? Gather the elts smaller than (first alon); gather those larger; sort; and combine: Quicksort: Take 2 ;; quicksort : list[num] 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)) ])) How did quicksort work? Gather the elts smaller than (first alon); gather those larger; sort; and combine: gather the larger elts gather the smaller elts [well write smallerthan, largerthan later] Quicksort: Take 2 ;; quicksort : list[num] list[num] ;; sorts a list of nums into increasing order (define (quicksort alon) (cond [(empty? alon) ] [(cons? alon) (quicksort (smallerthan (first alon) (rest alon)))...
View
Full
Document
 Fall '07
 Fisler
 Sort

Click to edit the document details