1 / 30

CS 312: Algorithm Analysis

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis. Lecture #10: Analysis of Quicksort. Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick. Announcements. HW #7 Due Today

mahala
Download Presentation

CS 312: Algorithm Analysis

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. This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #10: Analysis of Quicksort Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick

  2. Announcements • HW #7 Due Today • Questions about Non-homogeneous RR or Change-of-variable? • Project #2 • Early: today • Due: Friday

  3. Thought • "In theory, there is no difference between theory and practice. But, in practice, there is."    - Jan L.A. van de Snepscheut

  4. Objectives • Answer your questions about RRs • Review theoretical analysis of Quicksort • Worst case • Best case • Empirical analysis of Quicksort • Prepare for average case analysis

  5. Follow-up Questions: Recurrence Relations

  6. Efficient Sorting • Mergesort is • But inconvenient for implementation with arrays since we need space to merge • See footnote on next (hidden) slide for more details • Can we do better? Yes! • How does Quicksort work? • Pick a pivot element • Move everything smaller than the pivot to the left • And everything else to the right • Sort each side (recursively) in place

  7. Quicksort • Quicksort sorts in place • Example: Pivot about first element (3) • 3 1 4 1 5 9 2 6 5 3 5 8 9 --- before • 2 1 3 1 3 9 5 6 5 4 5 8 9 --- after • At most n swaps • Pivot element ends up in it’s final position • No element left or right of pivot will flip sides again • Sort each side independently • Recursive Divide and Conquer approach

  8. C.A.R. “Tony” Hoare • Invented Quicksort, Hoare logic, Z notation and CSP • Emeritus professor at Oxford • 1980 ACM Turing Award recipient • Knighted March 7, 2000 • Now works at Microsoft Research

  9. Quicksort procedureQuicksort (T[i..j]) if (j – i) < threshhold, theninsertionsort(T[i..j]) else p = choosepivotposition() p’ = pivot(T[i..j],p) // p’ is new position of pivot quicksort(T[i..p’-1]) quicksort(T[p’+1..j]) Divide the problem into sub-problemsbased on where the pivot ends up. What is missing / unclear?

  10. Choosing the largest (or smallest) element results in calls (levels), each call incurring a partitioning cost of order , for a total worst-case complexity of: Choosing a Good Pivot • This choice is the crux of an implementation • What would the worst case be? Partition Partition Partition Partition Partition

  11. Choosing a Good Pivot • This choice is the crux of an implementation • What would the best case be? Partition Splitting the input as evenly as possible results in sub-problems of size . The total number of levels then becomes , and the effort to partition each level is, for a total complexity

  12. Choosing a Good Pivot • How to choose a pivot? • The median is the best choice. • Why? • Why isn’t the median used in practice? • Not cheap to compute • Idea #1: Sort the array into non-decreasing order; then the median is the middle element. • Idea #2: Median finding algorithms

  13. Choosing a Good Pivot • As an alternative to the median, what could we choose? • The element in the middle position • Median of first, last, and middle • Median of k sampled elements • A random element • All of these pivots could yield unlucky (worst case) results or lucky (best case) results. • How likely are the worst case and the best case? • We really care about the average case behavior. • We’ll do that next time.

  14. Empirical Analysis • How do Mergesort and Quicksort compare? • Experiment #1: • Mergesort vs. Quicksort • Pivot: simple deterministic pivot -- pick element in first position • Data: random data

  15. Empirical Analysis

  16. Introduce

  17. Log Scale Plot

  18. Pick Suitable Constants

  19. Constants • What do those constants represent? • Conversion from stepsto seconds • What else? • The “hidden constant” dropped in the Big-O analysis

  20. Empirical Analysis • How do Mergesort and Quicksort compare? • Experiment #2: • Mergesort vs. Quicksort • Pivot: simple deterministic pivot -- pick element in first position • Data: simulated worst case (worst out of 50 random runs)

  21. Worst Case (in 50 samples) Which has the better worst case?

  22. Worst Case (in 50 samples) Which has the better worst case?

  23. Worst Case Asymptotic Bound You have to useyour imaginationa bit on this slide.

  24. Empirical Analysis • How does Quicksort perform with a random pivot? • Experiment #3: • Quicksort (with simple deterministic pivot)vs. Quicksort (with random pivot) • Data: sorted data

  25. Empirical Analysis: Data Time is in milliseconds. Platform: forgotten …

  26. Comparing Quicksort on Sorted Lists

  27. Log Scale:Comparing Quicksort on Sorted Lists

  28. Why Quicksort with Random Pivot? • Avoids the worst case on a completely sorted list. • Improves on worst case

  29. Average Case Analysis • How would you approach an average case analysis?

  30. Assignment • Read: Sections 2.4 and 2.5 • Project #2 due Friday • So no homework due

More Related