Notes on loops

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

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 Document

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

View Full Document

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

View Full Document

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

View Full Document

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.

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)) … ])) Let’s 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 that’s 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 aren’t 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 we’ll 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 [we’ll 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

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online