260 likes | 273 Views
Learn about quick sorting, bubble sorting, and insertion sorting techniques in computer science. Understand the efficiency and performance of each method with practical examples.
E N D
Lecture 12: QuickSorting Queen’s University, Belfast David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science
Menu • Bubble Sort • Quick Sort CS 200 Spring 2002
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)))))) CS 200 Spring 2002
These don’t depend on the length of the list, so we don’t care about them. (define (find-most cf lst) (insertlg (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) How much work is find-most? • Work to evaluate (find-most f lst)? • Evaluate (insertlg (lambda (c1 c2) …) lst) • Evaluate lst • Evaluate (car lst) find-most is (n) If we double the length of the list, we amount of work find-most does approximately doubles. CS 200 Spring 2002
Sorting (define (sort cf lst) (if (null? lst) lst (let ((most (find-most cf lst))) (cons most (sort cf (delete lst most)))))) • How much work is it to sort? • How many times does sort evaluate find-most? sort is (n2) If we double the length of the list, we amount of work sort does approximately quadruples. CS 200 Spring 2002
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. CS 200 Spring 2002
> (sort counter-lt (revintsto 10)) (1 2 3 4 5 6 7 8 9 10) > counter 55 > (set! counter 0) > (sort counter-lt (revintsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) > counter 210 > (set! counter 0) > (sort counter-lt (revintsto 40)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40) > counter 820 CS 200 Spring 2002
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: 29372 real time: 29372 gc time: 0 CS 200 Spring 2002
(n2) CS 200 Spring 2002
Better Sorting Algorithms • The sort we defined is “bubble sort” (n2) • Is there a faster way to sort? CS 200 Spring 2002
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 CS 200 Spring 2002
insertsort (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst))))) CS 200 Spring 2002
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)))))) CS 200 Spring 2002
How much work is insertsort? (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)))))) (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst))))) How many times does insertsort evaluate insertel? Worst case? Average case? ntimes (once for each element) insertel is (n) insertsort is (n2) CS 200 Spring 2002
> (insertsort counter-lt (revintsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) > counter 190 > (set! counter 0) > (insertsort counter-lt (intsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) > counter 19 > (set! counter 0) > (insertsort counter-lt (rand-int-list 20)) (0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92) > counter 104 CS 200 Spring 2002
> (set! counter 0) > (bubblesort counter-lt (intsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) > counter 210 > (set! counter 0) > (bubblesort counter-lt (rand-int-list 20)) (4 4 16 18 19 20 23 32 36 51 53 59 67 69 73 75 82 82 88 89) > counter 210 CS 200 Spring 2002
Bubblesort vs. Insertsort • Both are (n2) worst case (reverse list) • Both are (n2) average case (random) • But insert-sort is about twice as fast • Insertsort is (n) best case (ordered list) CS 200 Spring 2002
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? CS 200 Spring 2002
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? CS 200 Spring 2002
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)))))) CS 200 Spring 2002
filter (define (filter f lst) (insertlg (lambda (el rest) (if (f el) (cons el rest) rest)) lst null)) CS 200 Spring 2002
Quick Sort • Quick Sort (C. A. R. Hoare, 1962) • What if the input list is sorted? Worst Case: (n2) • What if the input list is random? Expected: (n log2n) CS 200 Spring 2002
> (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 CS 200 Spring 2002
Growth of time to sort random list n2 (bubblesort) n log2n (quicksort) CS 200 Spring 2002
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 CS 200 Spring 2002
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 CS 200 Spring 2002