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…