Lecture 15: Quicker Sorting CS150: Computer Science

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.
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 Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
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 CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
Class 7: List Procedures David Evans cs1120 Fall 2009.
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.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
School of Computing Clemson University Fall, 2012
Sorting Chapter 14.
Lecture 4: Metacircles Eval Apply David Evans
6.001 Jeopardy.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Week 15 – Monday CS221.
Week 12 - Wednesday CS221.
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Class 19: Think Globally, Mutate Locally CS150: Computer Science
CS216: Program and Data Representation
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Evaluation of List Implementations
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 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
8/04/2009 Many thanks to David Sun for some of the included slides!
Data Abstraction David Evans cs205: engineering software
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Week of May 28-June 1 Memorial Day No School Test SOL 4.4 Plants Test
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Class 33: Learning to Count CS200: Computer Science
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Linked lists Prof. Noah Snavely CS1114
Class 26: Modeling Computing CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans http://www.cs.virginia.edu/evans Lecture 15: Quicker Sorting CS150: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans

Exam 1 Handed out at end of Friday’s class, due at the beginning of Monday’s class Open non-human resources but no help from other people Covers everything through today including: Lectures 1-15, Book Chapters 2-7, PS 1-4 Chapter 8 (out today) is not covered (but understanding it may help you prepare for exam) Review Session, Weds 6:30 in Olsson 228E

Assuming, the procedure passed as cf has constant running time. Sorting Cost (define (best-first-sort lst cf) (if (null? lst) lst (let ((best (find-best lst cf))) (cons best (best-first-sort (delete lst best) cf))))) (define (find-best lst cf) (if (null? (cdr lst)) (car lst) (pick-better cf (car lst) (find-best (cdr lst) cf)))) The running time of best-first-sort is in Θ(n2) where n is the number of elements in the input list. Assuming, the procedure passed as cf has constant running time.

Divide and Conquer sorting? Best first 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

insert-sort (define (insert-sort lst cf) (if (null? lst) null (insert-one (car lst) (insert-sort (cdr lst) cf) cf)))

insert-one (define (insert-one el lst cf) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf)))))

How much work is insert-sort? (define (insert-sort lst cf) (if (null? lst) null (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (define (insert-one el lst cf) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf))))) How many times does insert-sort evaluate insert-one? running time of insert-one is in (n) n times (once for each element) insert-sort has running time in (n2) where n is the number of elements in the input list

Which is better? Is insert-sort faster than best-first-sort?

> (insert-sort < (revintsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 190 applications of < > (insert-sort < (intsto 20)) Requires 19 applications of < > (insert-sort < (rand-int-list 20)) (0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92) Requires 104 applications of <

> (best-first-sort < (intsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 210 applications of < > (best-first-sort < (rand-int-list 20)) (4 4 16 18 19 20 23 32 36 51 53 59 67 69 73 75 82 82 88 89)

best-first-sort vs. insert-sort Both are (n2) worst case (reverse list) Both are (n2) when sorting a randomly ordered list But insert-sort is about twice as fast insert-sort is (n) best case (ordered input list)

Can we do better? (insert-one < 88 (list 1 2 3 5 6 23 63 77 89 90)) Suppose we had procedures (first-half lst) (second-half lst) that quickly divided the list in two halves?

Charge Exam 1 is out Friday, due Monday Exam Review, Wednesday 6:30 in Olsson 228E