1 / 103

Quicksort and Randomized Algs

Quicksort and Randomized Algs. David Kauchak cs161 Summer 2009. Administrative. Homework 2 is out Staple homework! Put your name on each page Homework 1 solution will be up soon. What does it do?. A[r] is called the pivot

carlagood
Download Presentation

Quicksort and Randomized Algs

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. Quicksort and Randomized Algs David Kauchak cs161 Summer 2009

  2. Administrative • Homework 2 is out • Staple homework! • Put your name on each page • Homework 1 solution will be up soon

  3. What does it do?

  4. A[r] is called the pivot • Partitions the elements A[p…r-1] in to two sets, those ≤ pivot and those > pivot • Operates in place • Final result: p r pivot A > pivot ≤ pivot

  5. p r … 5 7 1 2 8 4 3 6 …

  6. p r i … 5 7 1 2 8 4 3 6 …

  7. j p r i … 5 7 1 2 8 4 3 6 …

  8. j p r i … 5 7 1 2 8 4 3 6 …

  9. i j p r … 5 7 1 2 8 4 3 6 …

  10. i j p r … 5 7 1 2 8 4 3 6 …

  11. i j p r … 5 7 1 2 8 4 3 6 …

  12. i j p r … 5 7 1 2 8 4 3 6 …

  13. i j p r … 5 7 1 2 8 4 3 6 …

  14. i j p r … 5 1 7 2 8 4 3 6 …

  15. i j p r … 5 1 7 2 8 4 3 6 …

  16. i j p r … 5 1 7 2 8 4 3 6 …

  17. i j p r … 5 1 2 7 8 4 3 6 …

  18. i j p r … 5 1 2 7 8 4 3 6 …

  19. i j p r … 5 1 2 7 8 4 3 6 … What’s happening?

  20. i j p r … 5 1 2 7 8 4 3 6 … ≤ pivot > pivot unprocessed

  21. i j p r … 5 1 2 7 8 4 3 6 …

  22. i j p r … 5 1 2 4 8 7 3 6 …

  23. i j p r … 5 1 2 4 3 7 8 6 …

  24. i j p r … 5 1 2 4 3 6 8 7 …

  25. i j p r … 5 1 2 4 3 6 8 7 …

  26. Is Partition correct? • Partitions the elements A[p…r-1] in to two sets, those ≤ pivot and those > pivot? • Loop Invariant:

  27. Is Partition correct? • Partitions the elements A[p…r-1] in to two sets, those ≤ pivot and those > pivot? • Loop Invariant: A[p…i] ≤ A[r] and A[i+1…j-1] > A[r]

  28. Proof by induction • Loop Invariant: A[p…i] ≤ A[r] and A[i+1…j-1] > A[r] • Base case: A[p…i] and A[i+1…j-1] are empty • Assume it holds for j -1 • two cases: • A[j] > A[r] • A[p…i] remains unchanged • A[i+1…j] contains one additional element, A[j] which is > A[r]

  29. Proof by induction • Loop Invariant: A[p…i] ≤ A[r] and A[i+1…j-1] > A[r] • 2nd case: • A[j] ≤ A[r] • i is incremented • A[i] swapped with A[j] – A[p…i] constains one additional element which is ≤ A[r] • A[i+1…j-1] will contain the same elements, except the last element will be the old first element

  30. Partition running time? • Θ(n)

  31. Quicksort

  32. 8 5 1 3 6 2 7 4

  33. 8 5 1 3 6 2 7 4

  34. 1 3 2 4 6 8 7 5

  35. 1 3 2 4 6 8 7 5

  36. 1 3 2 4 6 8 7 5

  37. 1 2 3 4 6 8 7 5

  38. 1 2 3 4 6 8 7 5

  39. 1 2 3 4 6 8 7 5

  40. 1 2 3 4 6 8 7 5

  41. 1 2 3 4 6 8 7 5

  42. 1 2 3 4 5 8 7 6 What happens here?

  43. 1 2 3 4 5 8 7 6

  44. 1 2 3 4 5 8 7 6

  45. 1 2 3 4 5 6 7 8

  46. 1 2 3 4 5 6 7 8

  47. Some observations • Divide and conquer: different than MergeSort – do the work before recursing • How many times is/can an element selected for as a pivot? • What happens after an element is selected as a pivot? 1 3 2 4 6 8 7 5

  48. Is Quicksort correct? • Assuming Partition is correct • Proof by induction • Base case: Quicksort works on a list of 1 element • Inductive case: • Assume Quicksort sorts arrays for arrays of smaller < n elements, show that it works to sort n elements • If partition works correctly then we have: • and, by our inductive assumption, we have: pivot sorted A sorted ≤ pivot > pivot

  49. Running time of Quicksort? • Worst case? • Each call to Partition splits the array into an empty array and n-1 array

  50. Quicksort: Worse case running time • When does this happen? • sorted • reverse sorted • near sorted/reverse sorted Θ(n2) Which is?

More Related