David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,

Slides:



Advertisements
Similar presentations
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
CS 171: Introduction to Computer Science II Quicksort.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
1 Lecture 16: Lists and vectors Binary search, Sorting.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
David Evans Lecture 13: Astrophysics and Cryptology CS200: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Class 7: List Procedures David Evans cs1120 Fall 2009.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
Lists and Sorting Algorithms
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Merge Sort.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Sorting by Tammy Bailey
David Kauchak CS52 – Spring 2015
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
CSE 143 Lecture 23: quick sort.
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Algorithmic complexity: Speed of algorithms
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Lecture 9: The Great Lambda Tree of Knowledge and Power
List and list operations (continue).
Algorithmic complexity: Speed of algorithms
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

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