sort sort
play

Sort Sort If g is in O(f) we say that f is an upper bound - PDF document

One-Slide Summary g is in O(f) iff there exist positive constants c and n 0 such that g( n ) c f( n ) for all n n 0 . Sort Sort If g is in O(f) we say that f is an upper bound Procedures Procedures for g. and and We use


  1. One-Slide Summary • g is in O(f) iff there exist positive constants c and n 0 such that g( n ) ≤ c f( n ) for all n ≥ n 0 . Sort Sort • If g is in O(f) we say that f is an upper bound Procedures Procedures for g. and and • We use Omega Ω for lower bounds and Theta Θ for tight bounds. Quicker Quicker • Knowing a running time is in O( f ) tells you that Sorting Sorting the running time is not worse than f . This can only be good news. • Some way to sort have different running times. #2 Outline Exam 1 • Handed out at end of Monday's class on Mar • Administrivia 01, due at the beginning of Wednesday’s class – Your views, voting. – You have two days, should take two hours • Sorting: timing and costs • Open Book – No DrScheme • Insertion Sort • Open TAs & Profs – No Friends • Better sorting? • Covers everything through this Wednesday including: – Lectures 1-11, Book Chapters 1-8, PS 1-4 • Review Session TBA #3 #4 Time On Problem Sets: The Good Time On Problem Sets: The Bad – “At least, personally, I could not have done this PS – “I believe this PS3 has taken me over 10 hours to without their help. Is that really what the complete, not including reading for class.” problem sets are supposed to be?” – “I'd say this lab took around a total of just over 3 • PS3 is one of the two hardest problem sets. hours for me, which is not too bad I suppose.” Remember, you are not expected to know or do it all. • Some people mentioned that they found the PS – 89% of you: perfect score on PS3, 79% on PS2. You may be working too hard! long: 10 hours was the max mentioned time. • PS Design: Open-Ended Grading, not Rote! – "One credit of laboratory work can equal one to four hours per week." - UVA Registrar http://www.virginia.edu/registrar/about.html – Final problems allow us to distinguish between – "a 3 hour course requires about 10 hours/week for the entire semester." - UVA Kinesiology http://records.uva.acalog.com/preview_program.php?catoid=11&poid=1052&bc=1 superstars: currently you are all superstars! – "a ratio of 4 clock hours per credit hour per week." - UVA Clinical Nursing http://www.nursing.virginia.edu/media/NEW%20Student%20Handbook%20CNL%2007-08.pdf – Example: Skipping 10-12 (convert-lcommands, – "Total contact hours for a course should account for readings, online time, outside rewrite-lcommands, fractal) on PS3: 20/25 preparation and study. Total contact hours required per credit hour are as follows: 135 hours for a 3-credit course [9 hours a week for 15 weeks]." - UVA Syllabus Template – Course curve : An “A” does not require perfect PS http://www.faculty.virginia.edu/bbcp/documents/Final_Syllabus_Template.doc #5 #6

  2. Tutoring and Hints TA Time Limit? – “It is absolutely ridiculous that my partner and I – “Is there any way to get one on one tutoring for had to wait an hour and forty minutes for help this type of problem set?” when we entered our our name on the wait list • In the past, the ACM and ACM-W have offered shortly after we arrived.” one-on-one tutoring. Send me (or the course • Recall that previously we voted for no time staff) email if you are interested; I will try to limit. We will vote again: set something up. • I believe each TA should spend some maximum – “More hints written into PS if possible please? This amount of time with each group (e.g., 10 way I can work on it independently of TAs” minutes) before moving on. A group that still • I will add more hints on a optional links for PS4 has questions after 10 minutes can add on. On your honor! themselves to the queue again. #7 #8 Recall: Asymptotic Complexity Writing The Code g is in O ( f ) iff: There are positive – “I'd rather have maybe 4 or 5 comprehensive constants c and n 0 such that questions where I wrote the entire snippet, g ( n ) ≤ cf ( n ) for all n ≥ n 0 . because I would get more chances to work off of my own code.” g is in Ω ( f ) iff: There are positive • Multiple people had this comment. Your wish is granted. Check out PS4, where there is no “fill constants c and n 0 such that in the blanks” code at all. g ( n ) ≥ cf ( n ) for all n ≥ n 0 . – “Also, 1 dropped problem set grade please!” g is in Θ ( f ) iff: g is in O ( f ) and g is • Nine people made such comments. Vote? – If so: drop lowest PS that is not the final project in Ω ( f ) . and that you got at least three points on. #9 #10 Is our sort good enough? Which of these is true? Takes over 1 second to sort 1000-length list. How long • Our sort procedure is too slow for VISA would it take to sort 1 million because its running time is in O ( n 2 ) items? • Our sort procedure is too slow for VISA 1s = time to sort 1000 4s ~ time to sort 2000 because its running time is in Ω ( n 2 ) 1M is 1000 * 1000 • Our sort procedure is too slow for VISA Sorting time is n 2 because its running time is in Θ ( n 2 ) so, sorting 1000 times as many items will take 1000 2 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. #11 #12

  3. Which of these is true? Liberal Arts Trivia: Art History • Name the work shown • Our sort procedure is too slow for VISA and its sculptor. The because its running time is in O ( n 2 ) artist is generally • Our sort procedure is too slow for VISA considered the because its running time is in Ω ( n 2 ) progenitor of modern sculpture: he departed • Our sort procedure is too slow for VISA from mythology and because its running time is in Θ ( n 2 ) allegory and modeled the human body with Knowing a running time is in O ( f ) tells you the running time is not worse than f . This can only be good news. It doesn’t tell you anything about realism, celebrating how bad it is. (Lots of people and books get this wrong.) individual character and physicality. #13 #14 Liberal Arts Trivia: Chinese History Sorting Cost • This period of Chinese history roughly (define ( best-first-sort lst cf) (if (null? lst) lst corresponds to the Eastern Zhou dynasty (8 th (let ((best (find-best lst cf))) century BCE to 5 th century BCE). China was (cons best (best-first-sort (delete lst best) cf))))) (define ( find-best lst cf) feudalistic, with Zhou kings controlling only (if (null? (cdr lst)) (car lst) the capital (Luoyang) and granting the rest as (pick-better cf (car lst) (find-best (cdr lst) cf)))) fiefdoms to several hundred nobles (including the Twelve Princes). As the era unfolded, The running time of best-first-sort is in Θ( n 2 ) where n is the number of elements in the input list. powerful states annexed smaller ones until a few large principalities controlled China. By 6 th Assuming the comparison function century BCE, the feudal system had crumbled passed as cf has constant running time. and the Warring States period had begun. #15 #16 Divide and Conquer sorting? insert-sort (define ( insert-sort lst cf) • Best first sort : find the lowest in the list, (if (null? lst) null add it to the front of the result of sorting (insert-one (car lst) the list after deleting the lowest. (insert-sort (cdr lst) cf) cf))) • Insertion sort : insert the first element of the list in the right place in the sorted rest Try writing insert-one. of the list. (define (insert-one element lst cf) ...) – Let's write this together! – Hint: use/write helper function insert-one (insert-one 2 (list 1 3 5) <) --> (1 2 3 5) – (insert-one 2 (list 1 3 4 5)) --> (1 2 3 4 5) #17 #18

  4. How much work is insert-sort? insert-one (define ( insert-sort lst cf) (define ( insert-one el lst cf) (if (null? lst) null (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (define ( insert-one el lst cf) (if (null? lst) (list el) (cons (car lst) (if (cf el (car lst)) (cons el lst) (insert-one el (cdr lst) cf))))) (cons (car lst) (insert-one el (cdr lst) cf))))) How many times does insert- sort evaluate insert-one? #19 #20 How much work is insert-sort? How much work is insert-sort? (define ( insert-sort lst cf) (define ( insert-sort lst cf) (if (null? lst) null (if (null? lst) null (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (define ( insert-one el lst cf) (define ( insert-one el lst cf) (if (null? lst) (list el) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf))))) (cons (car lst) (insert-one el (cdr lst) cf))))) How many times does insert- How many times does insert- running time of insert- running time of insert- sort evaluate insert-one? sort evaluate insert-one? one is ? one is in Θ ( n ) n times (once for each element) n times (once for each element) #21 #22 How much work is insert-sort? Which is better? (define ( insert-sort lst cf) (if (null? lst) null • Is insert-sort faster than best-first-sort? (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (define ( insert-one el lst cf) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf))))) How many times does insert- running time of insert- sort evaluate insert-one? one is in Θ ( n ) n times (once for each element) insert-sort has running time in Θ ( n 2 ) where n is the number of elements in the input list #23 #24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend