david evans cs.virginia/evans
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 PresentationTRANSCRIPT
![Page 1: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/1.jpg)
David Evanshttp://www.cs.virginia.edu/evans
Class 12: Quickest Sorting
CS150: Computer ScienceUniversity of VirginiaComputer Science
Rose Bushby Jacintha Henry & Rachel Kay
Dandelion of Defeatby Mary Elliott Neal
by Kristina Caudle
![Page 2: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/2.jpg)
2CS150 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 (n2)
![Page 3: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/3.jpg)
3CS150 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 (n2)
• To sort more efficiently, we need to divide the problem more evenly each step
![Page 4: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/4.jpg)
4CS150 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 asinsertsortexcept usesinsertelh
(if (= (length lst) 1) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((fh …
![Page 5: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/5.jpg)
5CS150 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)
![Page 6: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/6.jpg)
6CS150 Fall 2005: Lecture 12: Quickest Sorting
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)
![Page 7: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/7.jpg)
7CS150 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: (n2log n)
![Page 8: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/8.jpg)
8CS150 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)
![Page 9: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/9.jpg)
9CS150 Fall 2005: Lecture 12: Quickest Sorting
![Page 10: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/10.jpg)
10CS150 Fall 2005: Lecture 12: Quickest Sorting
The Great Lambda Treeof Ultimate Knowledgeand Infinite Power
![Page 11: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/11.jpg)
11CS150 Fall 2005: Lecture 12: Quickest Sorting
Sorted Binary Trees
el
A tree containingall elements x suchthat (cf x el) is true
A tree containingall elements x suchthat (cf x el) is false
left right
![Page 12: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/12.jpg)
12CS150 Fall 2005: Lecture 12: Quickest Sorting
Tree Example
5
2 8
741
3 cf: <
null null
![Page 13: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/13.jpg)
13CS150 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
tree must be a non-null tree
tree must be a non-null tree
![Page 14: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/14.jpg)
14CS150 Fall 2005: Lecture 12: Quickest Sorting
Trees as Lists
52 8
1 (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))
![Page 15: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/15.jpg)
15CS150 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 orright trees.
Otherwise, decideif el should be in the left or right subtree.insert it into thatsubtree, but leave theother subtree unchanged.
![Page 16: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/16.jpg)
16CS150 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 callinsertel-tree, the size ofthe tree. So, doublingthe size of the tree only increases the number ofcalls by 1!
insertel-tree is (log2 n)
log2 a = bmeans 2b = a
![Page 17: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/17.jpg)
17CS150 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 ()))
![Page 18: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/18.jpg)
18CS150 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))))))
![Page 19: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/19.jpg)
19CS150 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 ofelements in tree
(n log n)
n = number ofelements in lst
![Page 20: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/20.jpg)
20CS150 Fall 2005: Lecture 12: Quickest Sorting
0
2000
4000
6000
8000
10000
120002
10
18
26
34
42
50
58
66
74
82
90
98
n log2 ninsertsort-tree
n2 insertsort
Growth of time to sort random list
![Page 21: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/21.jpg)
21CS150 Fall 2005: Lecture 12: Quickest Sorting
Comparing sorts> (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)
![Page 22: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/22.jpg)
22CS150 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
![Page 23: David Evans cs.virginia/evans](https://reader035.vdocuments.us/reader035/viewer/2022062221/56813861550346895da01058/html5/thumbnails/23.jpg)
23CS150 Fall 2005: Lecture 12: Quickest Sorting
Charge• PS4 due Monday (1 week only!)• Next class:
– Finishing quicksort– Understanding the universe is (n3)
are there any harder problems?