Lecture 13 Of On and Off Grounds Sorting

  • Slides: 22
Download presentation
Lecture 13: Of On and Off Grounds Sorting Coffee Bean Sorting in Guatemala CS

Lecture 13: Of On and Off Grounds Sorting Coffee Bean Sorting in Guatemala CS 200: Computer Science David Evans University of Virginia 16 February 2004 CS 200 Spring 2004 http: //www. cs. virginia. edu/evans Computer Science

Menu • Find Most • Measuring Work • Sorting 16 February 2004 CS 200

Menu • Find Most • Measuring Work • Sorting 16 February 2004 CS 200 Spring 2004 2

find-mostcaffeinated (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl

find-mostcaffeinated (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval)))) (define (find-most-caffeinated menu) (insertl (lambda (c 1 c 2) (if (> (coffee-caffeine-rating c 1) (coffee-caffeine-rating c 2)) c 1 c 2)) (cdr menu) ; ; already used the car as stopval (car menu))) 16 February 2004 CS 200 Spring 2004 3

find-most-caffeinated (define (find-most-caff menu) (if (null? (cdr menu)) (car menu) (let ((rest-most-caff (find-most-caff (cdr

find-most-caffeinated (define (find-most-caff menu) (if (null? (cdr menu)) (car menu) (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)))) 16 February 2004 CS 200 Spring 2004 4

Comparing Processes > (trace >) (>) > (find-most-caff todays-menu) |(> 15 57) |#f |(>

Comparing Processes > (trace >) (>) > (find-most-caff todays-menu) |(> 15 57) |#f |(> 12 57) |#f |(> 92 57) |#t |(> 85 92) |#f ("BEN'S Expresso Noir Supreme" 17. 23. 92) 16 February 2004 > (find-most-caffeinated todays-menu) |(> 15 57) |#f |(> 12 57) |#f |(> 92 57) |#t |(> 85 92) |#f ("BEN'S Expresso Noir Supreme" 17. 23. 92) CS 200 Spring 2004 5

(define (find-most-caffeinated menu) (insertl (lambda (c 1 c 2) (if (> (coffee-caffeine-rating c 1)

(define (find-most-caffeinated menu) (insertl (lambda (c 1 c 2) (if (> (coffee-caffeine-rating c 1) (coffee-caffeine-rating c 2)) c 1 c 2)) (cdr menu) ; ; already used the car as stopval (car menu))) find-most (define (find-most cf lst) (insertl (lambda (c 1 c 2) (if (cf c 1 c 2)) (cdr lst) (car lst))) 16 February 2004 CS 200 Spring 2004 6

(define (find-most cf lst) (insertl (lambda (c 1 c 2) (if (cf c 1

(define (find-most cf lst) (insertl (lambda (c 1 c 2) (if (cf c 1 c 2)) (cdr lst) (car lst))) (define (find-most-caffeinated menu) (find-most (lambda (c 1 c 2) (> (coffee-caffeine-rating c 1) (coffee-caffeine-rating c 2))) menu)) 16 February 2004 CS 200 Spring 2004 7

How much work is find-most? 16 February 2004 CS 200 Spring 2004 8

How much work is find-most? 16 February 2004 CS 200 Spring 2004 8

Why not just time it? Moore’s Law: computing power doubles every 18 months! 16

Why not just time it? Moore’s Law: computing power doubles every 18 months! 16 February 2004 CS 200 Spring 2004 9

How much work is find-most? (define (find-most cf lst) (insertl (lambda (c 1 c

How much work is find-most? (define (find-most cf lst) (insertl (lambda (c 1 c 2) (if (cf c 1 c 2)) lst (car lst))) • Work to evaluate (find-most f lst)? – Evaluate (insertl (lambda (c 1 c 2) …) lst) These don’t depend on the length – Evaluate lst of the list, so we don’t care about – Evaluate (car lst) them. 16 February 2004 CS 200 Spring 2004 10

Work to evaluate insertl (define (insertl f lst stopval) (if (null? lst) stopval (f

Work to evaluate insertl (define (insertl f lst stopval) (if (null? lst) stopval (f (car lst) (insertl f (cdr lst) stopval)))) • How many times do we evaluate f for a list of length n? 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 week. ) 16 February 2004 CS 200 Spring 2004 11

Simple Sorting • We know how to find-most-caffeinated • How do we sort list

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 16 February 2004 CS 200 Spring 2004 12

sort-menu (define (sort-menu) (if (null? menu) menu (let ((most-caff (find-most-caffeinated menu))) (cons most-caff (sort-menu

sort-menu (define (sort-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) 16 February 2004 CS 200 Spring 2004 13

All Sorts (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf

All Sorts (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) 16 February 2004 CS 200 Spring 2004 14

Caffeine Sorts (define (sort-menu-by-caffeine menu) (sort (lambda (c 1 c 2) (> (coffee-caffeine-rating c

Caffeine Sorts (define (sort-menu-by-caffeine menu) (sort (lambda (c 1 c 2) (> (coffee-caffeine-rating c 1) (coffee-caffeine-rating c 2))) menu)) 16 February 2004 CS 200 Spring 2004 15

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst)))

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) (define (find-most cf lst) (insertl (lambda (c 1 c 2) (if (cf c 1 c 2)) lst (car lst))) • How much work is sort? • We measure work using orders of growth: How does work grow with problem size? 16 February 2004 CS 200 Spring 2004 16

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst)))

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) • What grows? – n = the number of elements in lst • How much work are the pieces? find-most is (n) delete is (n) • How many times does sort evaluate find-most and delete? 16 February 2004 CS 200 Spring 2004 17

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst)))

Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) • 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 sort is (n 2) If we double the length of the list, the amount of work approximately quadruples. 16 February 2004 CS 200 Spring 2004 18

Timing Sort > (time (sort < (revintsto 100))) cpu time: 20 real time: 20

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 16 February 2004 CS 200 Spring 2004 19

 (n 2) measured times = n 2/500 16 February 2004 CS 200 Spring

(n 2) measured times = n 2/500 16 February 2004 CS 200 Spring 2004 20

Is our sort good enough? Takes over 1 second to sort 1000 -length list.

Is our sort good enough? Takes over 1 second to sort 1000 -length list. How long would it take to sort 1 million items? 1 s = time to sort 1000 4 s ~ time to sort 2000 1 M is 1000 * 1000 (n 2) Sorting time is n 2 so, sorting 1000 times as many items will take 10002 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. 16 February 2004 CS 200 Spring 2004 21

Charge • Exam review in class Wednesday – Look over the practice exams from

Charge • Exam review in class Wednesday – Look over the practice exams from previous years before Wednesday • Exam 1 out Friday, due next Monday • Next Monday: faster ways of sorting • Read Tyson’s essay (before next Weds) – How does it relate to (n 2) – How does it relate to grade inflation 16 February 2004 CS 200 Spring 2004 22