David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.

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 16: Power Analysis.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
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.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
David Evans CS588: Security and Privacy University of Virginia Computer Science Lecture 15: Complexity Notes This is selected.
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.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
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 Lecture 15: Intractable Problems (Smiley.
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.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
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 23: Intractable Problems (Smiley Puzzles.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
David Kauchak CS52 – Spring 2015
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 2210 Discrete Structures Algorithms and Complexity
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Binary Search and Intro to Sorting
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
CS 2210 Discrete Structures Algorithms and Complexity
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Advanced Analysis of Algorithms
Lecture 9: The Great Lambda Tree of Knowledge and Power
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
David Kauchak cs161 Summer 2009
Lecture 15: Quicker Sorting CS150: Computer Science
Algorithm Analysis T(n) O() Growth Rates 5/21/2019 CS 303 – Big ‘Oh’
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting

23 February 2004CS 200 Spring Menu Measuring Work: What  really means Quicker Sorting

23 February 2004CS 200 Spring What does  really mean? O( x ) – it is no more than x work (upper bound)  ( x ) – work scales as x (tight bound)  ( x ) – it is at least x work (lower bound) If O( x ) and  ( x ) are true, then  ( x ) is true.

23 February 2004CS 200 Spring Meaning of O (“big Oh”) f(x) is O ( g (x) ) means: There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values.

23 February 2004CS 200 Spring O Examples f(x) is O ( g (x) ) means: There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values. x is O ( x 2 )? Yes, c = 1 works fine. 10x is O ( x )? Yes, c =.09 works fine. x 2 is O ( x )? No, no matter what c we pick, cx 2 > x for big enough x

23 February 2004CS 200 Spring Lower Bound:  (Omega) f(x) is  ( g (x) ) means: There is a positive constant c such that c * f(x) > g(x) for all but a finite number of x values. Difference from O – this was <

23 February 2004CS 200 Spring f(x) is  ( g (x) ) means: There is a positive constant c such that c * f(x) > g(x) for all but a finite number of x values. f(x) is O ( g (x) ) means: There is a positive constant c such that c * f(x) < g(x) for all but a finite number of x values. Examples x is  ( x ) –Yes, pick c = 2 10x is  ( x ) –Yes, pick c = 1 Is x 2  ( x )? –Yes! x is O( x ) –Yes, pick c =.5 10x is O( x ) –Yes, pick c =.09 x 2 is not O( x )

23 February 2004CS 200 Spring Tight Bound:  (Theta) f(x) is  ( g (x) ) iff: f(x) is O ( g (x) ) and f(x) is  ( g (x) )

23 February 2004CS 200 Spring  Examples 10x is  ( x ) –Yes, since 10x is  ( x ) and 10x is O( x ) Doesn’t matter that you choose different c values for each part; they are independent x 2 is/is not  ( x )? –No, since x 2 is not O ( x ) x is/is not  ( x 2 )? –No, since x 2 is not  ( x )

23 February 2004CS 200 Spring Sorting (define (TuttleSort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (TuttleSort cf (delete lst most)))))) (define (find-most cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) TuttleSort is  ( n 2 ) If we double the length of the list, we amount of work sort does approximately quadruples.

23 February 2004CS 200 Spring 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.

23 February 2004CS 200 Spring Divide and Conquer sorting? TuttleSort: find the lowest in the list, add it to the front of the result of sorting the list after deleting the lowest Insertion sort: insert the first element of the list in the right place in the sorted rest of the list

23 February 2004CS 200 Spring insertsort (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst)))))

23 February 2004CS 200 Spring insertel (define (insertel cf el lst) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insertel cf el (cdr lst))))))

23 February 2004CS 200 Spring How much work is insertsort? (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst))))) (define (insertel cf el lst) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insertel cf el (cdr lst)))))) Worst case? Average case? insertel is  ( n ) How many times does insertsort evaluate insertel ? n times (once for each element) insertsort is  ( n 2 )

23 February 2004CS 200 Spring > (insertsort < (revintsto 20)) ( ) Requires 190 applications of < > (insertsort < (intsto 20)) ( ) Requires 19 applications of < > (insertsort < (rand-int-list 20)) ( ) Requires 104 applications of <

23 February 2004CS 200 Spring > (bubblesort < (intsto 20)) ( ) Requires 210 applications of < > (bubblesort < (rand-int-list 20)) ( ) Requires 210 applications of <

23 February 2004CS 200 Spring TuttleSort vs. insertsort Both are  ( n 2 ) worst case (reverse list) Both are  ( n 2 ) average case (random) –But insert-sort is about twice as fast insertsort is  ( n ) best case (ordered list)

23 February 2004CS 200 Spring Can we do better? (insertel < 88 (list )) Suppose we had procedures (first-half lst) (second-half lst) that quickly divided the list in two halves?

23 February 2004CS 200 Spring Charge Read Tyson’s essay (before Wednesday) –How does it relate to  (n 2 ) –How does it relate to grade inflation –Don’t misinterpret it as telling you to run out and get tattoos and piercings! PS5: Auction Service –Introduces state and mutation: we will need to change our evaluation rules! –Don’t wait until we cover this in lecture (Friday) to get started (do the SICP reading!)