David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

Class 21: Imperative Programming University of Virginia cs1120 David Evans.
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 CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
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 31: Types of Types.
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.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
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 Lecture 3: Rules of Evaluation.
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.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
David Evans Lecture 13: Astrophysics and Cryptology CS200: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
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: Computers, Programs and Computing University of Virginia Computer Science Class 40: Web Site Presentations.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
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.
Lecture 3: Rules of Evaluation CS150: Computer Science
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 CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Lecture 4: Metacircles Eval Apply David Evans
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
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 11: All Sorts 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 13: Cost of Sorts CS150: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Class 26: Modeling Computing CS150: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee Bean Sorting in Guatemala

2 CS150 Fall 2005: Lecture 9: Sorting Menu PS2 Sorting PS3

3 CS150 Fall 2005: Lecture 9: Sorting Problem Sets Not just meant to review stuff you should already know –Get you to explore new ideas –Motivate what is coming up in the class The main point of the PSs is learning, not evaluation –Don’t give up if you can’t find the answer in the book –Discuss with other students

4 CS150 Fall 2005: Lecture 9: Sorting PS2: Question 3 Why is (define (higher-card? card1 card2) (> (card-rank card1) (card-rank card2) better than (define (higher-card? card1 card2) (> (car card1) (car card2)) ? In this class, we won’t worry too much about designing programs with good abstractions, since the programs we are dealing with are fairly small. For large programs, good abstractions are essential. That’s what most of CS201(J) is about.

5 CS150 Fall 2005: Lecture 9: Sorting PS2: Question 8, 9 Predict how long it will take Identify ways to make it faster Much of this week, and later classes will be focused on how computer scientists predict how long programs will take, and on how to make them faster.

6 CS150 Fall 2005: Lecture 9: Sorting Can we do better? (define (find-best-hand hole-cards community-cards) (car (sort higher-hand? (possible-hands hole-cards community-cards))))

7 CS150 Fall 2005: Lecture 9: Sorting find-best-hand (define (find-best-hand lst) (if (null? (cdr lst)) (car lst) (let ((rest-best (find-best-hand (cdr lst)))) (if (higher-hand? (car lst) rest-best) (car lst) rest-best))))

8 CS150 Fall 2005: Lecture 9: Sorting find-best-hand (define (find-best-hand lst) (insertl (lambda (hand1 hand2) (if (higher-hand? hand1 hand2) hand1 hand2)) (cdr lst) ;; already used the car as stopval (car lst))) (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval))))

9 CS150 Fall 2005: Lecture 9: Sorting find-best (define (find-best cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) (cdr lst) (car lst)))

10 CS150 Fall 2005: Lecture 9: Sorting (define (find-best cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) (cdr lst) (car lst))) (define (find-best-hand lst) (find-best higher-hand? lst))

11 CS150 Fall 2005: Lecture 9: Sorting How much work is find-best?

12 CS150 Fall 2005: Lecture 9: Sorting Why not just time it? Moore’s Law: computing power (used to) double every 18 months!

13 CS150 Fall 2005: Lecture 9: Sorting How much work is find-best? Work to evaluate (find-best f lst)? –Evaluate (insertl (lambda (c1 c2) …) lst) –Evaluate lst –Evaluate (car lst) (define (find-best cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) These don’t depend on the length of the list, so we don’t care about them.

14 CS150 Fall 2005: Lecture 9: Sorting Work to evaluate insertl How many times do we evaluate f for a list of length n ? (define (insertl f lst stopval) (if (null? lst) stopval (f (car lst) (insertl f (cdr lst) stopval)))) n insertl is  ( n )“Theta n” If we double the length of the list, we amount of work required approximately doubles. (We will see a more formal definition of  next class, and a more formal definition of “Amount of work” in November.)

15 CS150 Fall 2005: Lecture 9: Sorting Simple Sorting We know how to find-best How do we sort? Use (find-best lst) to find the best Remove it from the list Repeat until the list is empty

16 CS150 Fall 2005: Lecture 9: Sorting Simple Sort (define (sort cf lst) (if (null? lst) lst (let ((best (find-best cf lst))) (cons best (sort cf (delete lst best))))))

17 CS150 Fall 2005: Lecture 9: Sorting Sorting Hands (define (sort-hands lst) (sort higher-hand? lst))

18 CS150 Fall 2005: Lecture 9: Sorting Sorting How much work is sort? We measure work using orders of growth: How does work grow with problem size? (define (sort cf lst) (if (null? lst) lst (let ((most (find-best cf lst))) (cons most (sort cf (delete lst best)))))) (define (find-best cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst)))

19 CS150 Fall 2005: Lecture 9: Sorting Sorting What grows? –n = the number of elements in lst How much work are the pieces? find-best is  (n)delete is  (n) How many times does sort evaluate find-best and delete? (define (sort cf lst) (if (null? lst) lst (let ((best (find-best cf lst))) (cons best (sort cf (delete lst best))))))

20 CS150 Fall 2005: Lecture 9: Sorting Sorting n = the number of elements in lst find-most is  (n) delete is  (n) How many times does sort evaluate find-most and delete? n (define (sort cf lst) (if (null? lst) lst (let ((best (find-best cf lst))) (cons best (sort cf (delete lst best)))))) sort is  ( n 2 ) If we double the length of the list, the amount of work approximately quadruples.

21 CS150 Fall 2005: Lecture 9: Sorting Timing Sort > (time (sort < (revintsto 100))) cpu time: 20 real time: 20 gc time: 0 > (time (sort < (revintsto 200))) cpu time: 80 real time: 80 gc time: 0 > (time (sort < (revintsto 400))) cpu time: 311 real time: 311 gc time: 0 > (time (sort < (revintsto 800))) cpu time: 1362 real time: 1362 gc time: 0 > (time (sort < (revintsto 1600))) cpu time: 6650 real time: 6650 gc time: 0

22 CS150 Fall 2005: Lecture 9: Sorting (n2)(n2) = n 2 /500 measured times

23 CS150 Fall 2005: Lecture 9: Sorting Is our sort good enough? Takes over 1 second to sort 1000-length list. How long would it take to sort 1 million items? (n2)(n2) 1s = time to sort s ~ time to sort M is 1000 * 1000 Sorting time is n 2 so, sorting 1000 times as many items will take times as long = 1 million seconds ~ 11 days Note: there are 800 Million VISA cards in circulation. It would take 20,000 years to process a VISA transaction at this rate.

24 CS150 Fall 2005: Lecture 9: Sorting PS3: Lindenmayer System Fractals

25 CS150 Fall 2005: Lecture 9: Sorting L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence

26 CS150 Fall 2005: Lecture 9: Sorting L-System Rewriting Start: (F) Rewrite Rule: F  (F O(R30 F) F O(R-60 F) F) Work like BNF replacement rules, except replace all instances at once! Why is this a better model for biological systems? CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence

Level 0 (F) Level 1 F  (F O(R30 F) F O(R-60 F) F) Start: (F) (F O(R30 F) F O(R-60 F) F)

28 CS150 Fall 2005: Lecture 9: Sorting Level 2 Level 3

29 CS150 Fall 2005: Lecture 9: Sorting The Great Lambda Tree of Ultimate Knowledge and Infinite Power

30 CS150 Fall 2005: Lecture 9: Sorting CS Logo, Lincoln Hamilton and Dan Nguyen Proud Peacock, Marija Cvijetic and Raquel Johnathan Pink Buds by Kate Morandi and Erika Vogel Tie Dye by Bill Ingram The Fern of Serenity and Fractal Frustration, Nicole Numbers and Jon Carter Spirograph by Leah Nylen

31 CS150 Fall 2005: Lecture 9: Sorting Charge Wednesday: faster ways of sorting Read Tyson’s essay before Friday’s class –How does it relate to  (n 2 ) –How does it relate to grade inflation PS3 due Monday, Sept 19: lots more code for you to write than PS2