David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.

Slides:



Advertisements
Similar presentations
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
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 16: Power Analysis.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
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 40: Computing with Glue and Photons.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lecture 2 MAS 714 Hartmut Klauck
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 CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
1 Lecture 16: Lists and vectors Binary search, Sorting.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
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 10: Puzzling Pegboards.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
David Evans Lecture 13: Astrophysics and Cryptology CS200: Computer Science University of Virginia Computer Science.
David Evans CS150: Computer Science University of Virginia Computer Science Class 38: Googling.
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 Class 16: NP-Completeness / The Story so Far CS150: Computer Science University of Virginia Computer Science.
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.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
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.
Class 7: List Procedures David Evans cs1120 Fall 2009.
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.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
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 Class 26: Halting Problem It is plain at any.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Class 14: Intractable Problems 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 24: Metalinguistics CS200: Computer Science
Chapter 11 Limitations of Algorithm Power
Lecture 9: The Great Lambda Tree of Knowledge and Power
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
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 23: Computability CS200: Computer Science
Presentation transcript:

David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science Queen’s University, Belfast, Northern Ireland

2 CS150 Fall 2005: Lecture 13: Problems and Procedures Why are we spending so much time on sorting? Reason 1: its important Reason 2: it is a well defined problem for exploring algorithm design and complexity analysis A sensible programmer rarely (if ever) writes their own code for sorting – there are sort procedures provided by all major languages 800 pages long!

3 CS150 Fall 2005: Lecture 13: Problems and Procedures Art of Computer Programming, Donald E. Knuth Volume 1 (1968): Fundamental Algorithms Volume 2: Seminumerical Algorithms –Random numbers, arithmetic Volume 3: Sorting and Searching Volume 4: Combinatorial Algorithms (in preparation, 2005) Volume 5: Syntactic Algorithms (estimated for 2010) Volume 6, 7: planned The first finder of any error in my books receives $2.56; significant suggestions are also worth $0.32 each. If you are really a careful reader, you may be able to recoup more than the cost of the books this way.

4 CS150 Fall 2005: Lecture 13: Problems and Procedures Recap: insertsort-tree (define (insertel-tree cf el tree) (if (null? tree) (make-tree null el null) (if (cf el (get-element tree)) (make-tree (insertel-tree cf el (get-left tree)) (get-element tree) (get-right tree)) (make-tree (get-left tree) (get-element tree) (insertel-tree cf el (get-right tree)))))) (define (insertsort-tree cf lst) (define (insertsort-worker cf lst) (if (null? lst) null (insertel-tree cf (car lst) (insertsort-worker cf (cdr lst))))) (extract-elements (insertsort-worker cf lst)))  (log n ) n = number of elements in tree  ( n log n ) n = number of elements in lst

5 CS150 Fall 2005: Lecture 13: Problems and Procedures Can we do better? Making all those trees is a lot of work Can we divide the problem in two halves, without making trees?

6 CS150 Fall 2005: Lecture 13: Problems and Procedures Quicksort Sir C. A. R. (Tony) Hoare, 1962 Divide the problem into: –Sorting all elements in the list where (cf (car list) el) is true (it is < the first element) –Sorting all other elements (it is >= the first element) Will this do better?

7 CS150 Fall 2005: Lecture 13: Problems and Procedures Quicksort (define (quicksort cf lst) (if (null? lst) lst (append (quicksort cf (filter (lambda (el) (cf el (car lst))) (cdr lst))) (list (car lst)) (quicksort cf (filter (lambda (el) (not (cf el (car lst)))) (cdr lst))))))

8 CS150 Fall 2005: Lecture 13: Problems and Procedures How much work is quicksort? (define (quicksort cf lst) (if (null? lst) lst (append (quicksort cf (filter (lambda (el) (cf el (car lst))) (cdr lst))) (list (car lst)) (quicksort cf (filter (lambda (el) (not (cf el (car lst)))) (cdr lst)))))) What if the input list is sorted? Worst Case:  ( n 2 ) What if the input list is random? Expected:  ( n log 2 n )

9 CS150 Fall 2005: Lecture 13: Problems and Procedures Comparing sorts > (testgrowth insertsort-tree) n = 250, time = 20 n = 500, time = 80 n = 1000, time = 151 n = 2000, time = 470 n = 4000, time = 882 n = 8000, time = 1872 n = 16000, time = 9654 n = 32000, time = n = 64000, time = n = , time = ( ) > (testgrowth quicksort) n = 250, time = 20 n = 500, time = 80 n = 1000, time = 91 n = 2000, time = 170 n = 4000, time = 461 n = 8000, time = 941 n = 16000, time = 2153 n = 32000, time = 5047 n = 64000, time = n = , time = ( ) Both are  ( n log 2 n ) Absolute time of quicksort much faster

10 CS150 Fall 2005: Lecture 13: Problems and Procedures Good enough for VISA? n = , time = seconds to sort with quicksort  ( n log 2 n ) How long to sort 800M items? > (log 4) > (* (log )) > (/ (* (log )) 36) > (/ (* (log )) ) > (/ (* (log )) ) seconds ~ 4.5 days

11 CS150 Fall 2005: Lecture 13: Problems and Procedures Are there any procedures more complex than simulating the universe (  (n 3 ) ) ?

12 CS150 Fall 2005: Lecture 13: Problems and Procedures Permuted Sorting A (possibly) really dumb way to sort: –Find all possible orderings of the list (permutations) –Check each permutation in order, until you find one that is sorted Example: sort (3 1 2) All permutations: (3 1 2) (3 2 1) (2 1 3) (2 3 1) (1 3 2) (1 2 3) is-sorted? is-sorted? is-sorted?is-sorted?is-sorted?is-sorted?

13 CS150 Fall 2005: Lecture 13: Problems and Procedures permute-sort (define (permute-sort cf lst) (car (filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst))))

14 CS150 Fall 2005: Lecture 13: Problems and Procedures is-sorted? (define (is-sorted? cf lst) (or (null? lst) (= 1 (length lst)) (and (cf (car lst) (cadr lst)) (is-sorted? cf (cdr lst)))))

15 CS150 Fall 2005: Lecture 13: Problems and Procedures all-permutations (define (all-permutations lst) (flat-one (map (lambda (n) (if (= (length lst) 1) (list lst) ; The permutations of (a) are ((a)) (map (lambda (oneperm) (cons (nth lst n) oneperm)) (all-permutations (exceptnth lst n))))) (intsto (length lst)))))

16 CS150 Fall 2005: Lecture 13: Problems and Procedures > (time (permute-sort <= (rand-int-list 5))) cpu time: 10 real time: 10 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 6))) cpu time: 40 real time: 40 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 7))) cpu time: 261 real time: 260 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 8))) cpu time: 3585 real time: 3586 gc time: 0 ( ) > (time (permute-sort <= (rand-int-list 9))) Crashes!

17 CS150 Fall 2005: Lecture 13: Problems and Procedures How much work is permute-sort? We evaluated is-sorted? once for each permutation of lst. How much work is is-sorted?? (n)(n) How many permutations of the list are there? (define (permute-sort cf lst) (car (filter (lambda (lst) (is-sorted? cf lst)) (all-permutations lst))))

18 CS150 Fall 2005: Lecture 13: Problems and Procedures Number of permutations There are n = (length lst) values in the first map, for each possible first element Then, we call all-permutations on the list without that element (length = n – 1 ) There are n * n – 1 * … * 1 permutations Hence, there are n! lists to check:  ( n! ) (map (lambda (n) (if (= (length lst) 1) (list lst) (map (lambda (oneperm) (cons (nth lst n) oneperm)) (all-permutations (exceptnth lst n))))) (intsto (length lst)))

19 CS150 Fall 2005: Lecture 13: Problems and Procedures Are there any procedures more complex than simulating the universe (  (n 3 ) ) ? Maybe this is the wrong question…

20 CS150 Fall 2005: Lecture 13: Problems and Procedures Procedures and Problems So far we have been talking about procedures (how much work is permute- sort?) We can also talk about problems: how much work is sorting? A problem defines a desired output for a given input. A solution to a problem is a procedure for finding the correct output for all possible inputs.

21 CS150 Fall 2005: Lecture 13: Problems and Procedures The Sorting Problem Input: a list and a comparison function Output: a list such that the elements are the same elements as the input list, but in order so that the comparison function evaluates to true for any adjacent pair of elements

22 CS150 Fall 2005: Lecture 13: Problems and Procedures Problems and Procedures If we know a procedure that is that is  ( f(n)) that solves a problem then we know the problem is O ( f(n)). The sorting problem is O ( n! ) since we know a procedure (permute-sort) that solves it in  ( n! ) Is the sorting problem is  ( n! ) ? No, we would need to prove there is no better procedure.

23 CS150 Fall 2005: Lecture 13: Problems and Procedures Problems and Procedures Sorting problem is O(n log n) –We know a procedure (quicksort) that solves sorting in  (n log n) Is the sorting problem  (n log n) ? –To know this, we need to prove there is no procedure that solves the sorting problem with time complexity better than  (n log n)

24 CS150 Fall 2005: Lecture 13: Problems and Procedures Sorting problem is Ω(n log n) There are n! possible orderings Each comparison can eliminate at best ½ of them So, best possible sorting procedure is Ω(log 2 n!) Sterling’s approximation: n! = Ω(n n ) –So, best possible sorting procedure is Ω(log (n n )) = Ω(n log n) Recall log multiplication is normal addition: log mn = log m + log n

25 CS150 Fall 2005: Lecture 13: Problems and Procedures Problems and Procedures Sorting problem is  (n log n) –We know a procedure (quicksort) that solves sorting in  (n log n) –We know there is no faster procedure since best sorting procedure is Ω(n log n) This is unusual: there are very few problems for which we know  –It is “easy” to get O for a problem: just find a procedure that solves it –It is extraordinarily difficult to get Ω for most problems: need to reason about all possible procedures

26 CS150 Fall 2005: Lecture 13: Problems and Procedures Charge Next class: –Some problems that are “hard” No procedure is known that is less complex than simulating the universe –Introduce the most famous and important open problem in Computer Science Are these really hard problems? Will return PS3 Friday PS4: Due Monday