1 / 26

Lecture 12: Quick Sorting Queen’s University, Belfast

This lecture discusses the QuickSort algorithm, its implementation, and its performance compared to other sorting algorithms. Topics covered include bubble sort, insertion sort, and the divide and conquer approach of QuickSort.

cstrong
Download Presentation

Lecture 12: Quick Sorting Queen’s University, Belfast

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12: QuickSorting Queen’s University, Belfast David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science

  2. Menu • Bubble Sort • Quick Sort CS 200 Spring 2002

  3. 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

  4. 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

  5. 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

  6. 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

  7. > (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

  8. 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

  9. (n2) CS 200 Spring 2002

  10. Better Sorting Algorithms • The sort we defined is “bubble sort” (n2) • Is there a faster way to sort? CS 200 Spring 2002

  11. 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

  12. insertsort (define (insertsort cf lst) (if (null? lst) null (insertel cf (car lst) (insertsort cf (cdr lst))))) CS 200 Spring 2002

  13. 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

  14. 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

  15. > (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

  16. > (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

  17. 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

  18. 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

  19. 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

  20. 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

  21. filter (define (filter f lst) (insertlg (lambda (el rest) (if (f el) (cons el rest) rest)) lst null)) CS 200 Spring 2002

  22. 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

  23. > (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

  24. Growth of time to sort random list n2 (bubblesort) n log2n (quicksort) CS 200 Spring 2002

  25. 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

  26. 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

More Related