David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University, Belfast
13 February 2002CS 200 Spring Menu Bubble Sort Quick Sort
13 February 2002CS 200 Spring Sort (from last time) (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most))))))
13 February 2002CS 200 Spring How much work is find-most? Work to evaluate (find-most f lst)? –Evaluate (insertlg (lambda (c1 c2) …) lst) –Evaluate lst –Evaluate (car lst) (define (find-most cf lst) (insertlg (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) These don’t depend on the length of the list, so we don’t care about them. find-most is ( n ) If we double the length of the list, we amount of work find-most does approximately doubles.
13 February 2002CS 200 Spring Sorting How much work is it to sort? –How many times does sort evaluate find-most ? (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) sort is ( n 2 ) If we double the length of the list, we amount of work sort does approximately quadruples.
13 February 2002CS 200 Spring Counting (define counter 0) (define (counter-lt v1 v2) (set! counter (+ counter 1)) (< v1 v2)) (define (revintsto n) (if (= n 0) null (cons n (revintsto (- n 1))))) Using state to count number of evaluations. Will cover later.
13 February 2002CS 200 Spring > (sort counter-lt (revintsto 10)) ( ) > counter 55 > (set! counter 0) > (sort counter-lt (revintsto 20)) ( ) > counter 210 > (set! counter 0) > (sort counter-lt (revintsto 40)) ( ) > counter 820
13 February 2002CS 200 Spring Timing Sort > (time (sort < (revintsto 100))) cpu time: 20 real time: 20 gc time: 0 > (time (sort < (revintsto 200))) cpu time: 80 real time: 80 gc time: 0 > (time (sort < (revintsto 400))) cpu time: 311 real time: 311 gc time: 0 > (time (sort < (revintsto 800))) cpu time: 1362 real time: 1362 gc time: 0 > (time (sort < (revintsto 1600))) cpu time: 6650 real time: 6650 gc time: 0 > (time (sort < (revintsto 3200))) cpu time: real time: gc time: 0
13 February 2002CS 200 Spring (n2)(n2)
13 February 2002CS 200 Spring Better Sorting Algorithms The sort we defined is “bubble sort” ( n 2 ) Is there a faster way to sort?
13 February 2002CS 200 Spring Divide and Conquer sorting? Bubble sort: find the lowest in the list, add it to the front of the result of sorting the list after deleting the lowest Insertion sort: insert the first element of the list in the right place in the sorted rest of the list
13 February 2002CS 200 Spring insertsort (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst)))))
13 February 2002CS 200 Spring insertel (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))))))
13 February 2002CS 200 Spring How much work is insertsort? (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)))))) Worst case? Average case? insertel is ( n ) How many times does insertsort evaluate insertel ? n times (once for each element) insertsort is ( n 2 )
13 February 2002CS 200 Spring > (insertsort counter-lt (revintsto 20)) ( ) > counter 190 > (set! counter 0) > (insertsort counter-lt (intsto 20)) ( ) > counter 19 > (set! counter 0) > (insertsort counter-lt (rand-int-list 20)) ( ) > counter 104
13 February 2002CS 200 Spring > (set! counter 0) > (bubblesort counter-lt (intsto 20)) ( ) > counter 210 > (set! counter 0) > (bubblesort counter-lt (rand-int-list 20)) ( ) > counter 210
13 February 2002CS 200 Spring Bubblesort vs. Insertsort Both are ( n 2 ) worst case (reverse list) Both are ( n 2 ) average case (random) –But insert-sort is about twice as fast Insertsort is ( n ) best case (ordered list)
13 February 2002CS 200 Spring Can we do better? How well does insertsort divide the problem? Not so well – always divides into first element and rest of list. Can we divide the problem in a more even way?
13 February 2002CS 200 Spring Quicksort Divide the problem into: –Sorting all elements in the list where (cf (car list) el) is true (it is < the first element) –Sorting all elements in the list where (not (cf (car list) el) is true (it is >= the first element) Will this do better?
13 February 2002CS 200 Spring Quicksort (define (quicksort cf lst) (if (null? lst) lst (append (quicksort cf (filter (lambda (el) (cf el (car lst))) (cdr lst))) (list (car lst)) (quicksort cf (filter (lambda (el) (not (cf el (car lst)))) (cdr lst))))))
13 February 2002CS 200 Spring filter (define (filter f lst) (insertlg (lambda (el rest) (if (f el) (cons el rest) rest)) lst null))
13 February 2002CS 200 Spring Quick Sort Quick Sort (C. A. R. Hoare, 1962) What if the input list is sorted? Worst Case: ( n 2 ) What if the input list is random? Expected: ( n log 2 n )
13 February 2002CS 200 Spring > (define r1000 (rand-int-list 1000)) > (time (sort < r1000)) cpu time: 1372 real time: 1372 gc time: 0 > (time (quicksort < r1000)) cpu time: 71 real time: 70 gc time: 0 > (define r2000 (rand-int-list 2000)) > (time (sort < r2000)) cpu time: 5909 real time: 5909 gc time: 0 > (time (quicksort < r2000)) cpu time: 180 real time: 180 gc time: 0 > (time (quicksort < (revintsto 1000))) cpu time: 2684 real time: 2684 gc time: 0
13 February 2002CS 200 Spring n log 2 n (quicksort) n 2 (bubblesort) Growth of time to sort random list
13 February 2002CS 200 Spring Reading Handout Optional, but I hope you will read it How orders of growth and Moore’s effect our understanding of the universe Why the grade deflation crisis needs to be addressed –Students get twice as smart every 15 years, but average GPAs have gone up less than 20%! Rose Center for Earth and Space, Director Neil DeGrasse Tyson
13 February 2002CS 200 Spring Charge PS4 –No new Computer Science concepts –You should be able to do it now –Lots of practice with lists and recursion Office Hours –None today (cancelled usual) –Friday after class (but back at my office) Friday: Cryptography