David Evans cs.virginia/evans

  • Published on
    04-Jan-2016

  • View
    22

  • Download
    2

Embed Size (px)

DESCRIPTION

Class 12: Quickest Sorting. David Evans http://www.cs.virginia.edu/evans. Dandelion of Defeat by Mary Elliott Neal. Rose Bush by Jacintha Henry & Rachel Kay. by Kristina Caudle. CS150: Computer Science University of Virginia Computer Science. Insert Sort. (define (insertel cf el lst) - PowerPoint PPT Presentation

Transcript

  • David Evanshttp://www.cs.virginia.edu/evansClass 12: Quickest SortingCS150: Computer ScienceUniversity of VirginiaComputer ScienceRose Bushby Jacintha Henry & Rachel KayDandelion of Defeatby Mary Elliott Nealby Kristina Caudle

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    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 (n2)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Divide and ConquerBoth simplesort and insertsort divide the problem of sorting a list of length n into:Sorting a list of length n-1Doing the right thing with one elementHence, there are always n stepsAnd since each step is (n), they are (n2) To sort more efficiently, we need to divide the problem more evenly each step

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    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 asinsertsortexcept usesinsertelh(if (= (length lst) 1) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((fh

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    append cant 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 listMaking a list of length k requires k conses, so append must be (n + m)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Experimental Confirmation> (testgrowthappend)n = 10000, m = 1, time = 0n = 20000, m = 1, time = 0n = 40000, m = 1, time = 0n = 80000, m = 1, time = 10n = 160000, m = 1, time = 10n = 320000, m = 1, time = 40n = 640000, m = 1, time = 70n = 1280000, m = 1, time = 150n = 2560000, m = 1, time = 290(1.0 4.0 1.75 2.14286 1.9333)n = 10000, m = 10000, time = 0n = 20000, m = 20000, time = 0n = 40000, m = 40000, time = 0n = 80000, m = 80000, time = 10n = 160000, m = 160000, time = 50n = 320000, m = 320000, time = 40n = 640000, m = 640000, time = 70n = 1280000, m = 1280000, time = 150n = 2560000, m = 2560000, time = 270(5.0 0.8 1.75 2.142857142857143 1.8)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    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 insertelhlog n applications of insertelhEach one is (n) work since append is (n)Complexity is: (n2log n)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Making it fasterWe need to either:Reduce the number of applications of insertelh in insertsorth

    Reduce the number of applications of insertelh in insertelh

    Reduce the time for one application of insertelh

    Impossible need to consider each elementUnlikelyeach application already halves the listNeed to make first-half, second-half and append faster than (n)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    The Great Lambda Treeof Ultimate Knowledgeand Infinite Power

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Sorted Binary TreeselA tree containingall elements x suchthat (cf x el) is trueA tree containingall elements x suchthat (cf x el) is falseleftright

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Tree Example5287413cf: (testgrowth simplesort)n = 250, time = 110n = 500, time = 371n = 1000, time = 2363n = 2000, time = 8162n = 4000, time = 31757(3.37 6.37 3.45 3.89)> (testgrowth insertsort)n = 250, time = 40n = 500, time = 180n = 1000, time = 571n = 2000, time = 2644n = 4000, time = 11537(4.5 3.17 4.63 4.36)> (testgrowth insertsorth)n = 250, time = 251n = 500, time = 1262n = 1000, time = 4025n = 2000, time = 16454n = 4000, time = 66137(5.03 3.19 4.09 4.02)> (testgrowth insertsort-tree)n = 250, time = 30n = 500, time = 250n = 1000, time = 150n = 2000, time = 301n = 4000, time = 1001(8.3 0.6 2.0 3.3)

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    Can we do better?Making all those trees is a lot of workCan we divide the problem in two halves, without making trees?

    Continues in Lecture 13

    Lecture 12: Quickest Sorting

    *CS150 Fall 2005: *

    ChargePS4 due Monday (1 week only!)Next class: Finishing quicksortUnderstanding the universe is (n3) are there any harder problems?

    Lecture 12: Quickest Sorting