Notes on loops

Notes on loops - Higher-Order Functions and Loops c. Kathi...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Higher-Order Functions and Loops c. Kathi Fisler, 2001-2004 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 insertion-sort. What happened? With templates, you write programs according to the natural recursion Insertion-sort 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 catch-all 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) (smaller-than (first alon) (rest alon)) (larger-than (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 smaller-than, larger-than 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 (smaller-than (first alon) (rest alon)))...
View Full Document

Page1 / 58

Notes on loops - Higher-Order Functions and Loops c. Kathi...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online