Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11: All Sorts CS200: Computer Science University of Virginia

Similar presentations


Presentation on theme: "Lecture 11: All Sorts CS200: Computer Science University of Virginia"— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans
Lecture 11: All Sorts CS200: Computer Science University of Virginia Computer Science David Evans

2 find-most-caffeinated Sorting
Menu find-most-caffeinated Sorting 11 February 2002 CS 200 Spring 2002

3 find-most-caffeinated
(define (find-most-caffienated menu) (insertlg (lambda (c1 c2) (if (> (coffee-caffeine-rating c1) (coffee-caffeine-rating c2)) c1 c2)) menu (make-coffee "null-coffee" ))) ;; we should only get the null-coffee ;; if there are no coffees on the menu 11 February 2002 CS 200 Spring 2002

4 find-most-caffeinated
(define (find-most-caff menu) (if (null? menu) (make-coffee "null-coffee" ) (let ((rest-most-caff (find-most-caff (cdr menu)))) (if (> (coffee-caffeine-rating (car menu)) (coffee-caffeine-rating rest-most-caff)) (car menu) rest-most-caff)))) 11 February 2002 CS 200 Spring 2002

5 Comparing Processes > (trace >) (>)
> (find-most-caff todays-menu) |(> 57 -1) |#t |(> 15 57) |#f |(> 12 57) |(> 92 57) |(> 85 92) ("BEN'S Expresso Noir Supreme" ) > (find-most-caffienated todays-menu) |(> 57 -1) |#t |(> 15 57) |#f |(> 12 57) |(> 92 57) |(> 85 92) ("BEN'S Expresso Noir Supreme" ) 11 February 2002 CS 200 Spring 2002

6 Sorting 11 February 2002 CS 200 Spring 2002

7 Simple Sorting We know how to find-most-caffeinated
How do we sort list by caffeine rating? Use (find-most-caffeinated menu) to find the most caffeinated Remove it from the menu Repeat until the menu is empty 11 February 2002 CS 200 Spring 2002

8 Delete ;;; Evaluates to the list parameter with
;;; exactly one instance of el removed. (define (delete lst el) (if (null? lst) (error "Element not found!") (if (eq? (car lst) el) (cdr lst) (cons (car lst) (delete (cdr lst) el))))) 11 February 2002 CS 200 Spring 2002

9 sort-menu (define (sort-menu menu) (if (null? menu) menu
(let ((most-caff (find-most-caffeinated menu))) (cons most-caff (sort-menu (delete menu most-caff)))))) How can we generalize this? (e.g., sort by price also) 11 February 2002 CS 200 Spring 2002

10 All Sorts (define (sort cf lst) (if (null? lst) lst
(let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) 11 February 2002 CS 200 Spring 2002

11 Caffeine Sorts (define (sort-menu-by-caffeine menu)
(sort (lambda (c1 c2) (> (coffee-caffeine-rating c1) (coffee-caffeine-rating c2))) menu)) 11 February 2002 CS 200 Spring 2002

12 find-most (define (find-most cf lst) (insertlg (lambda (c1 c2)
(if (cf c1 c2) c1 c2)) lst (car lst))) 11 February 2002 CS 200 Spring 2002

13 Sorting How much work is it to sort?
We measure work using orders of growth How does work grow with problem size? 11 February 2002 CS 200 Spring 2002

14 Why not just time/count absolute amount of work?
11 February 2002 CS 200 Spring 2002

15 Computing Power 1969-2002 (in Apollo Control Computer Units)
Moore’s Law: computing power doubles every 18 months!

16 How much work is find-most?
(define (find-most cf lst) (insertlg (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) Work to evaluate (find-most f lst)? Evaluate (insertlg (lambda (c1 c2) …) lst) Evaluate lst Evaluate (car lst) These don’t depend on the length of the list, so we don’t care about them. 11 February 2002 CS 200 Spring 2002

17 Work to evaluate insertlg
(define (insertlg f lst start) (if (null? lst) start (f (car lst) (insertlg f (cdr lst) start)))) How many times do we evaluate f for a list of length n? n insertlg is (n) If we double the length of the list, we amount of work insertlg does approximately doubles. 11 February 2002 CS 200 Spring 2002

18 Charge PS3 Due Wednesday Continue how much work is sorting Wednesday
Lab Hours: Tonight, 7:30-9 PM in Small Hall Continue how much work is sorting Wednesday Think about other ways of sorting 11 February 2002 CS 200 Spring 2002


Download ppt "Lecture 11: All Sorts CS200: Computer Science University of Virginia"

Similar presentations


Ads by Google