Download presentation
Presentation is loading. Please wait.
Published byJoella Gray Modified over 9 years ago
1
David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University, Belfast
2
13 February 2002CS 200 Spring 20022 Menu Bubble Sort Quick Sort
3
13 February 2002CS 200 Spring 20023 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))))))
4
13 February 2002CS 200 Spring 20024 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.
5
13 February 2002CS 200 Spring 20025 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.
6
13 February 2002CS 200 Spring 20026 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.
7
13 February 2002CS 200 Spring 20027 > (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
8
13 February 2002CS 200 Spring 20028 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
9
13 February 2002CS 200 Spring 20029 (n2)(n2)
10
13 February 2002CS 200 Spring 200210 Better Sorting Algorithms The sort we defined is “bubble sort” ( n 2 ) Is there a faster way to sort?
11
13 February 2002CS 200 Spring 200211 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
12
13 February 2002CS 200 Spring 200212 insertsort (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst)))))
13
13 February 2002CS 200 Spring 200213 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))))))
14
13 February 2002CS 200 Spring 200214 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 )
15
13 February 2002CS 200 Spring 200215 > (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
16
13 February 2002CS 200 Spring 200216 > (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
17
13 February 2002CS 200 Spring 200217 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)
18
13 February 2002CS 200 Spring 200218 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?
19
13 February 2002CS 200 Spring 200219 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?
20
13 February 2002CS 200 Spring 200220 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))))))
21
13 February 2002CS 200 Spring 200221 filter (define (filter f lst) (insertlg (lambda (el rest) (if (f el) (cons el rest) rest)) lst null))
22
13 February 2002CS 200 Spring 200222 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 )
23
13 February 2002CS 200 Spring 200223 > (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
24
13 February 2002CS 200 Spring 200224 n log 2 n (quicksort) n 2 (bubblesort) Growth of time to sort random list
25
13 February 2002CS 200 Spring 200225 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
26
13 February 2002CS 200 Spring 200226 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.