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

Slides:



Advertisements
Similar presentations
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Class 7: List Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Binomial Priority Queues
6.001 SICP Data abstractions
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 21: Inheritance CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Class 37: Making Lists, Numbers and Recursion from Glue Alone
Lecture 26: The Metacircular Evaluator Eval Apply
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 9: The Great Lambda Tree of Knowledge and Power
Class 33: Learning to Count CS200: Computer Science
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Binomial Priority Queues
Lecture 15: Crazy Eddie and the Fixed Points Background
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
CS 403: Programming Languages
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

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

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

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" 0.00 -1))) ;; we should only get the null-coffee ;; if there are no coffees on the menu 11 February 2002 CS 200 Spring 2002

find-most-caffeinated (define (find-most-caff menu) (if (null? menu) (make-coffee "null-coffee" 0.00 -1) (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

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" 17.23 . 92) > (find-most-caffienated todays-menu) |(> 57 -1) |#t |(> 15 57) |#f |(> 12 57) |(> 92 57) |(> 85 92) ("BEN'S Expresso Noir Supreme" 17.23 . 92) 11 February 2002 CS 200 Spring 2002

Sorting 11 February 2002 CS 200 Spring 2002

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

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

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

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

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

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

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

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

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

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

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

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