Download Lecture Slides on Quickest Sorting | CS 1120 and more Study notes Computer Science in PDF only on Docsity!
Greg Humphreys
http://www.cs.virginia.edu/~humper
Class 12:
Quickest
Sorting
CS150: Computer Science University of Virginia Computer Science
Nicole Buxhoeveden Laura Messinger
Drew Thompson
Matt Spear Stephen Jay Guy
Liz White Chris Holland
Alice Jackson
CS150 Fall 2006: Lecture 12: Quickest Sorting 2
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
CS150 Fall 2006: Lecture 12: Quickest Sorting (^3)
Divide and Conquer
- Both simplesort and insertsort divide the
problem of sorting a list of length n into:
- Sorting^ a^ list^ of length^ n -
- Doing the right thing with one element
- Hence, there are always n steps
- And since each step is! ( n ), they are! ( n
)
- To sort more efficiently, we need to divide
the problem more evenly each step
CS150 Fall 2006: Lecture 12: Quickest Sorting (^4)
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 …
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)
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 = 160000, m = 1, time = 10
n = 320000, m = 1, time = 40
n = 640000, m = 1, time = 70
n = 1280000, m = 1, time = 150
n = 2560000, m = 1, time = 290
(1.0 4 .0 1. 75 2.14286 1.9333)
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 = 160000, m = 160000, time = 50
n = 320000, m = 320000, time = 40
n = 640000, m = 640000, time = 70
n = 1280000, m = 1280000, time = 150
n = 2560000, m = 2560000, time = 270
(5.0 0.8 1. 75 2.142857142857143 1.8)
CS150 Fall 2006: Lecture 12: Quickest Sorting 7
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 first-half is "( n )
Complexity is: "( n
log n )
CS150 Fall 2006: Lecture 12: Quickest Sorting 8
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 )
CS150 Fall 2006: Lecture 12: Quickest Sorting (^9)
Sorted Binary Trees
el
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
CS150 Fall 2006: Lecture 12: Quickest Sorting (^10)
Tree Example
cf: <
null null
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
tree must be a non-null tree
tree must be a non-null tree
Trees as Lists
(make-tree (make-tree (make-tree null 1 null)
null)
(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))
CS150 Fall 2006: Lecture 12: Quickest Sorting 19
Comparing sorts
> (testgrowth simplesort)
n = 250, time = 110
n = 500, time = 371
n = 1000, time = 2363
n = 2000, time = 8162
n = 4000, time = 31757
> (testgrowth insertsort)
n = 250, time = 40
n = 500, time = 180
n = 1000, time = 571
n = 2000, time = 2644
n = 4000, time = 11537
> (testgrowth insertsorth)
n = 250, time = 251
n = 500, time = 1262
n = 1000, time = 4025
n = 2000, time = 16454
n = 4000, time = 66137
> (testgrowth insertsort-tree)
n = 250, time = 30
n = 500, time = 250
n = 1000, time = 150
n = 2000, time = 301
n = 4000, time = 1001
CS150 Fall 2006: Lecture 12: Quickest Sorting 20
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
CS150 Fall 2006: Lecture 12: Quickest Sorting (^21)
Charge
- PS4 due Monday (1 week only!)
- Next class:
- Finishing quicksort
- Understanding^ the^ universe^ is^ "( n
) are
there any harder problems?