David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.

Slides:



Advertisements
Similar presentations
David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 17: ProvingUndecidability.
Advertisements

Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
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 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
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 14: Asymptotic Growth.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
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.
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.
CSIT 402 Data Structures II
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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 Class 26: Halting Problem It is plain at any.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Merge Sort.
Lecture 4: Metacircles Eval Apply David Evans
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
COP4020 Programming Languages
MergeSort Source: Gibbs & Tamassia.
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 #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Data Structures: Searching
Lecture 9: The Great Lambda Tree of Knowledge and Power
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Search Sorted Array: Binary Search Linked List: Linear Search
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha Henry & Rachel Kay Dandelion of Defeat by Mary Elliott Neal by Kristina Caudle

2 CS150 Fall 2005: Lecture 12: Quickest Sorting Insert Sort (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)))))) insertsort is  ( n 2 )

3 CS150 Fall 2005: Lecture 12: Quickest Sorting Divide and Conquer Both simplesort and insertsort divide the problem of sorting a list of length n into: –Sorting a list of length n-1 –Doing the right thing with one element Hence, there are always n steps –And since each step is  ( n ), they are  ( n 2 ) To sort more efficiently, we need to divide the problem more evenly each step

4 CS150 Fall 2005: Lecture 12: Quickest Sorting Insert Halves (define (insertelh cf el lst) (if (null? lst) (list el) (let ((fh (first-half lst)) (sh (second-half lst))) (if (cf el (car fh)) (append (cons el fh) sh) (if (null? sh) (append fh (list el)) (if (cf el (car sh)) (append (insertelh cf el fh) sh) (append fh (insertelh cf el sh)))))))) (define (insertsorth cf lst) (if (null? lst) null (insertelh cf (car lst) (insertsorth cf (cdr lst))))) Same as insertsort except uses insertelh (if (= (length lst) 1) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((fh …

5 CS150 Fall 2005: Lecture 12: Quickest Sorting append can’t be O(1) Needs to make a new list of length n + m where n is the length of the first list, and m is the length of the second list Making a list of length k requires k conses, so append must be  (n + m)

6 CS150 Fall 2005: Lecture 12: Quickest Sorting Experimental Confirmation > (testgrowthappend) n = 10000, m = 1, time = 0 n = 20000, m = 1, time = 0 n = 40000, m = 1, time = 0 n = 80000, m = 1, time = 10 n = , m = 1, time = 10 n = , m = 1, time = 40 n = , m = 1, time = 70 n = , m = 1, time = 150 n = , m = 1, time = 290 ( ) n = 10000, m = 10000, time = 0 n = 20000, m = 20000, time = 0 n = 40000, m = 40000, time = 0 n = 80000, m = 80000, time = 10 n = , m = , time = 50 n = , m = , time = 40 n = , m = , time = 70 n = , m = , time = 150 n = , m = , time = 270 ( )

7 CS150 Fall 2005: Lecture 12: Quickest Sorting Complexity of Insert Half Sort (define (insertelh cf el lst) (if (null? lst) (list el) (if (= (length lst) 1) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((fh (first-half lst)) (sh (second-half lst))) (if (cf el (car sh)) (append (insertelh cf el fh) sh) (append fh (insertelh cf el sh))))))) (define (insertsorth cf lst) (if (null? lst) null (insertelh cf (car lst) (insertsorth cf (cdr lst))))) n applications of insertelh log n applications of insertelh Each one is  ( n) work since append is  (n) Complexity is:  (n 2 log n)

8 CS150 Fall 2005: Lecture 12: Quickest Sorting Making it faster We need to either: 1.Reduce the number of applications of insertelh in insertsorth 2.Reduce the number of applications of insertelh in insertelh 3.Reduce the time for one application of insertelh Impossible – need to consider each element Unlikely…each application already halves the list Need to make first-half, second-half and append faster than  (n)

9 CS150 Fall 2005: Lecture 12: Quickest Sorting

10 CS150 Fall 2005: Lecture 12: Quickest Sorting The Great Lambda Tree of Ultimate Knowledge and Infinite Power

11 CS150 Fall 2005: Lecture 12: Quickest Sorting Sorted Binary Trees elel A tree containing all elements x such that (cf x el) is true A tree containing all elements x such that (cf x el) is false left right

12 CS150 Fall 2005: Lecture 12: Quickest Sorting Tree Example cf: < null

13 CS150 Fall 2005: Lecture 12: Quickest Sorting Representing Trees (define (make-tree left el right) (list left el right)) (define (get-left tree) (first tree)) (define (get-element tree) (second tree)) (define (get-right tree) (third tree)) left and right are trees ( null is a tree) tree must be a non-null tree

14 CS150 Fall 2005: Lecture 12: Quickest Sorting Trees as Lists (make-tree (make-tree (make-tree null 1 null) 2 null) 5 (make-tree null 8 null)) (define (make-tree left el right) (list left el right)) (define (get-left tree) (first tree)) (define (get-element tree) (second tree)) (define (get-right tree) (third tree))

15 CS150 Fall 2005: Lecture 12: Quickest Sorting insertel-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)))))) If the tree is null, make a new tree with el as its element and no left or right trees. Otherwise, decide if el should be in the left or right subtree. insert it into that subtree, but leave the other subtree unchanged.

16 CS150 Fall 2005: Lecture 12: Quickest Sorting How much work is insertel-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)))))) Each time we call insertel-tree, the size of the tree. So, doubling the size of the tree only increases the number of calls by 1! insertel-tree is  (log 2 n ) log 2 a = b means 2 b = a

17 CS150 Fall 2005: Lecture 12: Quickest Sorting insertsort-tree (define (insertsort-worker cf lst) (if (null? lst) null (insertel-tree cf (car lst) (insertsort-worker cf (cdr lst))))) (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst))))) No change…but insertsort-worker evaluates to a tree not a list! (((() 1 ()) 2 ()) 5 (() 8 ()))

18 CS150 Fall 2005: Lecture 12: Quickest Sorting extract-elements We need to make a list of all the tree elements, from left to right. (define (extract-elements tree) (if (null? tree) null (append (extract-elements (get-left tree)) (cons (get-element tree) (extract-elements (get-right tree))))))

19 CS150 Fall 2005: Lecture 12: Quickest Sorting Complexity of 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

20 CS150 Fall 2005: Lecture 12: Quickest Sorting n log 2 n insertsort-tree n 2 insertsort Growth of time to sort random list

21 CS150 Fall 2005: Lecture 12: Quickest Sorting Comparing sorts > (testgrowth simplesort) n = 250, time = 110 n = 500, time = 371 n = 1000, time = 2363 n = 2000, time = 8162 n = 4000, time = ( ) > (testgrowth insertsort) n = 250, time = 40 n = 500, time = 180 n = 1000, time = 571 n = 2000, time = 2644 n = 4000, time = ( ) > (testgrowth insertsorth) n = 250, time = 251 n = 500, time = 1262 n = 1000, time = 4025 n = 2000, time = n = 4000, time = ( ) > (testgrowth insertsort-tree) n = 250, time = 30 n = 500, time = 250 n = 1000, time = 150 n = 2000, time = 301 n = 4000, time = 1001 ( )

22 CS150 Fall 2005: Lecture 12: Quickest Sorting Can we do better? Making all those trees is a lot of work Can we divide the problem in two halves, without making trees? Continues in Lecture 13

23 CS150 Fall 2005: Lecture 12: Quickest Sorting Charge PS4 due Monday (1 week only!) Next class: –Finishing quicksort –Understanding the universe is  ( n 3 ) are there any harder problems?