David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,

Slides:



Advertisements
Similar presentations
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
Advertisements

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.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
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.
Chapter 19: Searching and Sorting Algorithms
CS 280 Data Structures Professor John Peterson. Project “Tree 1” Questions? Let’s look at my test cases.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
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 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
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.
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 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 Class 19: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
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.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Merge Sort.
Data Abstraction: Sets
Recursive Objects (Part 4)
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
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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
singleRightRotation 
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
Lecture 9: The Great Lambda Tree of Knowledge and Power
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
More Scheme CS 331.
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University, Belfast, Northern Ireland

3 March 2004CS 200 Spring TuttleSort (define (TuttleSort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (TuttleSort cf (delete lst most)))))) (define (find-most cf lst) (insertl (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) TuttleSort is  ( n 2 ) If we double the length of the list, we amount of work sort does approximately quadruples.

3 March 2004CS 200 Spring 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 March 2004CS 200 Spring Divide and Conquer Both TuttleSort 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

3 March 2004CS 200 Spring Can we do better? (insertel < 88 (list )) Suppose we had procedures (first-half lst) (second-half lst) that quickly divided the list in two halves?

3 March 2004CS 200 Spring 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))))))))

3 March 2004CS 200 Spring Evaluating insertelh > (insertelh < 3 (list )) |(insertelh # 3 ( )) | (< 3 1) | #f | (< 3 5) | #t | (insertelh # 3 (1 2 4)) | |(< 3 1) | |#f | |(< 3 4) | |#t | |(insertelh # 3 (1 2)) | | (< 3 1) | | #f | | (< 3 2) | | #f | | (insertelh # 3 (2)) | | |(< 3 2) | | |#f | | (2 3) | |(1 2 3) | ( ) |( ) ( ) (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)))))))) Every time we call insertelh, the size of the list is approximately halved!

3 March 2004CS 200 Spring How much work is insertelh? Suppose first-half and second-half are  (1) (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)))))))) Each time we call insertelh, the size of lst halves. So, doubling the size of the list only increases the number of calls by 1. List Size Number of insertelh applications

3 March 2004CS 200 Spring How much work is insertelh? Suppose first-half and second-half are  (1) Each time we call insertelh, the size of lst halves. So, doubling the size of the list only increases the number of calls by 1. List Size Number of insertelh applications insertelh is  (log 2 n ) log 2 a = b means 2 b = a

3 March 2004CS 200 Spring insertsorth (define (insertsorth cf lst) (if (null? lst) null (insertelh cf (car lst) (insertsorth cf (cdr lst))))) insertsorth would be  ( n log 2 n ) if we have fast first-half / second-half Same as insertsort, except uses insertelh (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))))))))

3 March 2004CS 200 Spring Is there a fast first-half procedure? No! To produce the first half of a list length n, we need to cdr down the first n /2 elements So: –first-half is  (n) –insertelh calls first-half every time…so –insertelh is  (n) *  (log 2 n) =  (n log 2 n) –insertsorth is  (n) *  (n log 2 n) =  (n 2 log 2 n) Yikes! We’ve done all this work, and its still worse than our simple TuttleSort !

3 March 2004CS 200 Spring

3 March 2004CS 200 Spring The Great Lambda Tree of Ultimate Knowledge and Infinite Power

3 March 2004CS 200 Spring 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

3 March 2004CS 200 Spring Tree Example cf: < null

3 March 2004CS 200 Spring 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

3 March 2004CS 200 Spring 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))

3 March 2004CS 200 Spring 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.

3 March 2004CS 200 Spring 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

3 March 2004CS 200 Spring 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 ()))

3 March 2004CS 200 Spring 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))))))

3 March 2004CS 200 Spring How much work is insertsort-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)))  ( n log 2 n )  ( n ) applications of insertel-tree each is  ( log n )

3 March 2004CS 200 Spring n log 2 n insertsort-tree n 2 TuttleSort Growth of time to sort random list

3 March 2004CS 200 Spring Comparing sorts > (testgrowth tuttlesort) 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 ( )

3 March 2004CS 200 Spring 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 21…