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 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 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 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) (smaller-than (first alon) (rest alon)) (larger-than (first alon) (rest alon)) ])) gather the larger elts gather the smaller elts [well write smaller-than, larger-than 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.

### 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