UVA CS 150 - Lecture 9: Of On and Off Grounds Sorting

Unformatted text preview:

1David Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 9: Of On and Off Grounds SortingCoffee Bean Sorting in Guatemala2CS150 Fall 2005: Lecture 9: SortingMenu• PS2• Sorting• PS33CS150 Fall 2005: Lecture 9: SortingProblem 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 students4CS150 Fall 2005: Lecture 9: SortingPS2: Question 3Why 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.5CS150 Fall 2005: Lecture 9: SortingPS2: Question 8, 9• Predict how long it will take• Identify ways to make it fasterMuch 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.6CS150 Fall 2005: Lecture 9: SortingCan we do better?(define (find-best-hand hole-cards community-cards)(car (sort higher-hand? (possible-hands hole-cards community-cards))))27CS150 Fall 2005: Lecture 9: Sortingfind-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))))8CS150 Fall 2005: Lecture 9: Sortingfind-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)))) 9CS150 Fall 2005: Lecture 9: Sortingfind-best(define (find-best cf lst)(insertl(lambda (c1 c2) (if (cf c1 c2) c1 c2))(cdr lst)(car lst)))10CS150 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))11CS150 Fall 2005: Lecture 9: SortingHow much work is find-best?12CS150 Fall 2005: Lecture 9: SortingWhy not just time it?05000001000000150000020000002500000300000035000004000000450000019691971197219741975197719781980198119831984198619871989199019921993199519961998199920012002Moore’s Law: computing power (used to) double every 18 months!313CS150 Fall 2005: Lecture 9: SortingHow 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.14CS150 Fall 2005: Lecture 9: SortingWork to evaluate insertl• How many times do we evaluate ffor a list of length n?(define (insertl f lst stopval)(if (null? lst) stopval(f (car lst) (insertl f (cdr lst) stopval))))ninsertl 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.)15CS150 Fall 2005: Lecture 9: SortingSimple 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 empty16CS150 Fall 2005: Lecture 9: SortingSimple Sort(define (sort cf lst)(if (null? lst) lst(let ((best (find-best cf lst)))(cons best (sort cf(delete lst best))))))17CS150 Fall 2005: Lecture 9: SortingSorting Hands(define (sort-hands lst)(sort higher-hand? lst))18CS150 Fall 2005: Lecture 9: SortingSorting• 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)))419CS150 Fall 2005: Lecture 9: SortingSorting• 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))))))20CS150 Fall 2005: Lecture 9: SortingSorting•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 Θ(n2)If we double the length of the list, the amount of work approximatelyquadruples.21CS150 Fall 2005: Lecture 9: SortingTiming 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: 022CS150 Fall 2005: Lecture 9: SortingΘ(n2)050001000015000200002500030000350000 1000 2000 3000= n2/500measured times23CS150 Fall 2005: Lecture 9: SortingIs our sort good enough?Takes over 1 second to sort 1000-length list. How long would it take to sort 1 million items?Θ(n2)1s = time to sort 10004s ~ time to sort 20001M is 1000 * 1000Sorting time is n2so, sorting 1000 times as many items will take 10002 times as long = 1 million seconds ~ 11 daysNote: there are 800 Million VISA cards in circulation.It would take 20,000 years to process a VISA transaction at this rate.24CS150 Fall 2005: Lecture 9: SortingPS3:Lindenmayer System Fractals525CS150 Fall 2005: Lecture 9: SortingL-SystemsCommandSequence::= ( CommandList)CommandList::= Command CommandListCommandList::=Command::= FCommand::= RAngleCommand::= OCommandSequence26CS150 Fall 2005: Lecture 9: SortingL-System RewritingStart: (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::= (


View Full Document

UVA CS 150 - Lecture 9: Of On and Off Grounds Sorting

Documents in this Course
Objects

Objects

6 pages

Load more
Download Lecture 9: Of On and Off Grounds Sorting
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 9: Of On and Off Grounds Sorting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9: Of On and Off Grounds Sorting 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?