data structures in java
play

Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel - PowerPoint PPT Presentation

Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel Bauer 1 Quick Sort Another divide-and-conquer algorithm. Pick any pivot element v. Partition the array into elements x v and x v. Recursively sort the


  1. Partitioning the Array ¡ ¡ ¡ ¡public ¡static ¡void ¡quicksort(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(right ¡> ¡left) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡v ¡= ¡find_pivot_index(a, ¡left, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡int ¡j ¡= ¡right-­‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ // ¡move ¡pivot ¡to ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp ¡= ¡a[v]; ¡a[v] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡{ ¡ // ¡partition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++i] ¡< ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++j] ¡> ¡v) ¡{}; ¡ O(N) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(i ¡>= ¡j) ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[j]; ¡a[j] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ // ¡move ¡pivot ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ //recursively ¡sort ¡both ¡partitions ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡quicksort(a,left, ¡i-­‑1); ¡ ¡quicksort(a,i+1, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} 12

  2. Quick Sort: Worst Case • Running time depends on the how the pivot partitions the array. • Worst case: Pivot is always the smallest or largest element. One of the partitions is empty! 34 8 64 2 51 32 21 1 . 
 . 
 13 .

  3. Quick Sort: Worst Case • Running time depends on the how the pivot partitions the array. • Worst case: Pivot is always the smallest or largest element. One of the partitions is empty! 34 8 64 2 51 32 21 1 1 34 8 64 2 51 21 32 . 
 . 
 13 .

  4. Quick Sort: Worst Case • Running time depends on the how the pivot partitions the array. • Worst case: Pivot is always the smallest or largest element. One of the partitions is empty! 34 8 64 2 51 32 21 1 1 34 8 64 2 51 21 32 2 34 8 64 51 32 21 . 
 . 
 13 .

  5. Quick Sort: Worst Case 34 8 64 2 51 32 21 1 1 34 8 64 2 51 32 21 8 2 34 64 51 32 21 ⁞ 51 64 51 64 T(1) = 1 14

  6. Quick Sort: Worst Case 34 8 64 2 51 32 21 1 1 34 8 64 2 51 32 21 8 2 34 64 51 32 21 ⁞ 51 64 T(2) = T(1) + 2 51 64 T(1) = 1 Time for partitioning 14

  7. Quick Sort: Worst Case 34 8 64 2 51 32 21 1 1 34 8 64 2 51 32 21 8 2 34 64 51 32 21 T(N-2) = T(N-3) + (N-2) ⁞ 51 64 T(2) = T(1) + 2 51 64 T(1) = 1 Time for partitioning 14

  8. Quick Sort: Worst Case 34 8 64 2 51 32 21 1 T(N-1) = T(N-2) + (N-1) 1 34 8 64 2 51 32 21 8 2 34 64 51 32 21 T(N-2) = T(N-3) + (N-2) ⁞ 51 64 T(2) = T(1) + 2 51 64 T(1) = 1 Time for partitioning 14

  9. Quick Sort: Worst Case 34 8 64 2 51 32 21 1 T(N) = T(N-1) + N T(N-1) = T(N-2) + (N-1) 1 34 8 64 2 51 32 21 8 2 34 64 51 32 21 T(N-2) = T(N-3) + (N-2) ⁞ 51 64 T(2) = T(1) + 2 51 64 T(1) = 1 Time for partitioning 14

  10. Quick Sort: Worst Case 15

  11. Quick Sort: Worst Case 15

  12. Quick Sort: Worst Case 15

  13. Quick Sort: Worst Case 15

  14. Quick Sort: Worst Case 15

  15. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 16

  16. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 8 2 1 21 34 64 51 32 16

  17. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 8 2 1 21 34 64 51 32 1 2 8 16

  18. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 8 2 1 21 34 64 51 32 1 2 8 34 32 51 64 16

  19. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 T(N) = 2 T(N/2) +N 8 2 1 21 34 64 51 32 1 2 8 34 32 51 64 (we ignore the pivot element, so this overestimates the running time slightly) 17

  20. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 T(N) = 2 T(N/2) +N 8 2 1 21 34 64 51 32 T(N/2) = 2 T(N/4) +N/2 1 2 8 34 32 51 64 (we ignore the pivot element, so this overestimates the running time slightly) 17

  21. Quick Sort: Best Case • Best case: Pivot is always the median element. 
 Both partitions have about the same size. 34 8 64 2 51 32 21 1 T(N) = 2 T(N/2) +N 8 2 1 21 34 64 51 32 T(N/2) = 2 T(N/4) +N/2 ⁞ 1 2 8 34 32 51 64 T(1) = 1 (we ignore the pivot element, so this overestimates the running time slightly) 17

  22. Quick Sort: Best Case (note that this is the same analysis as for Merge Sort) 18

  23. Quick Sort: Best Case (note that this is the same analysis as for Merge Sort) 18

  24. Quick Sort: Best Case (note that this is the same analysis as for Merge Sort) 18

  25. Quick Sort: Best Case assume (note that this is the same analysis as for Merge Sort) 18

  26. Quick Sort: Best Case assume (note that this is the same analysis as for Merge Sort) 18

  27. Quick Sort: Best Case assume (note that this is the same analysis as for Merge Sort) 18

  28. Choosing the Pivot 19

  29. Choosing the Pivot • Ideally we want to choose the median in each partition, but we don’t know where it is! 19

  30. Choosing the Pivot • Ideally we want to choose the median in each partition, but we don’t know where it is! • Computing the pivot should be a constant time operation. 19

  31. Choosing the Pivot • Ideally we want to choose the median in each partition, but we don’t know where it is! • Computing the pivot should be a constant time operation. • Choosing the element at the beginning/end/middle is a terrible idea! 
 Better: Choose a random element. 19

  32. Choosing the Pivot • Ideally we want to choose the median in each partition, but we don’t know where it is! • Computing the pivot should be a constant time operation. • Choosing the element at the beginning/end/middle is a terrible idea! Better: Choose a random element. • Good approximation for median: “Median-of-three” 19

  33. Choosing a Pivot: 
 Median of Three Choose the median of array[0], array[n]m and array[n/2]. 34 8 64 2 51 32 21 1 20

  34. Choosing a Pivot: 
 Median of Three Choose the median of array[0], array[n]m and array[n/2]. 34 8 64 2 51 32 21 1 1 2 34 8 64 51 32 21 20

  35. Choosing a Pivot: 
 Median of Three Choose the median of array[0], array[n]m and array[n/2]. 34 8 64 2 51 32 21 1 1 2 34 8 64 51 32 21 8 32 21 34 64 51 20

  36. Choosing a Pivot: 
 Median of Three Choose the median of array[0], array[n]m and array[n/2]. 34 8 64 2 51 32 21 1 1 2 34 8 64 51 32 21 8 32 21 34 64 51 8 21 32 20

  37. Median of Three ¡ ¡ ¡ ¡public ¡static ¡int ¡find_pivot_index(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡center ¡= ¡( ¡left ¡+ ¡right ¡) ¡/ ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[center] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[center]; ¡a[center] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[center]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[center]; ¡a[center] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡center; ¡ ¡ ¡ ¡ ¡} 21

  38. 
 
 Analyzing Quick Sort • Worst case running time: 
 • Best and average case (random pivot): 
 • Is QuickSort stable? 
 • Space requirement? 
 22

  39. 
 
 Analyzing Quick Sort • Worst case running time: 
 • Best and average case (random pivot): 
 • Is QuickSort stable? 
 No. Partitioning can change order of elements. 
 (but can make QuickSort stable). • Space requirement? 
 22

  40. 
 
 Analyzing Quick Sort • Worst case running time: 
 • Best and average case (random pivot): 
 • Is QuickSort stable? 
 No. Partitioning can change order of elements. 
 (but can make QuickSort stable). • Space requirement? 
 In-place O(1), but the method activation stack grows with the running time. O(N) 22

  41. Comparison-Based Sorting Algorithms T Worst T Best T Avg Space Stable? ✓ Insertion Sort ✗ Shell Sort ✗ Heap Sort ✓ Merge Sort ✗ Quick Sort gray entries: not shown in class *depends on increment sequence 23

  42. Comparison-Based Sorting Algorithms T Worst T Best T Avg worst case lower Space Stable? bound on comparison based general sorting! ✓ Insertion Sort Can we do better if we make some assumptions? ✗ Shell Sort ✗ Heap Sort ✓ Merge Sort ✗ Quick Sort gray entries: not shown in class *depends on increment sequence 23

  43. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 24

  44. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 25

  45. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 0 0 0 0 0 0 1 0 0 1 2 3 4 5 6 7 8 9 26

  46. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 1 0 0 0 0 0 1 0 0 1 2 3 4 5 6 7 8 9 27

  47. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 1 1 0 0 0 0 1 0 0 1 2 3 4 5 6 7 8 9 28

  48. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 2 1 0 0 0 0 1 0 0 1 2 3 4 5 6 7 8 9 29

  49. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 2 1 1 0 0 0 1 0 0 1 2 3 4 5 6 7 8 9 30

  50. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 1 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 31

  51. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 32

  52. Bucket Sort • Assume we know there are M possible values. • Keep an array count of length M . • Scan through the input array A and for each i O(N) increment count [ A i ]. 
 A 1 8 2 3 2 4 6 1 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 32

  53. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 33

  54. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 34

  55. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 35

  56. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 36

  57. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 37

  58. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 38

  59. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 6 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 39

  60. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 6 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 40

  61. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 6 8 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 41

  62. Bucket Sort • Then iterate through count.F or each i write count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 6 8 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 42

  63. Bucket Sort • Then iterate through count.F or each i write O(M) count [ i ] copies of i to A . 
 A 1 1 2 2 3 4 6 8 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 42

  64. Bucket Sort • Then iterate through count.F or each i write O(M) count [ i ] copies of i to A . 
 Total time for Bucket Sort:O(N +M) A 1 1 2 2 3 4 6 8 count 0 2 2 1 1 0 1 0 1 0 0 1 2 3 4 5 6 7 8 9 42

  65. Radix Sort • Generalization of Bucket sort for Large M. • Assume M contains all base b numbers up to b p -1 
 (e.g. all base-10 integers up to 10 3 ) • Do p passes over the data, using Bucket Sort for each digit. • Bucket sort is stable! 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 43

  66. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 2 • Bucket sort according 3 to least significant digit. 4 064 5 6 7 8 9 44

  67. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 2 • Bucket sort according 3 to least significant digit. 4 064 5 6 7 008 8 9 45

  68. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 2 • Bucket sort according 3 to least significant digit. 4 064 5 216 6 7 008 8 9 46

  69. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 512 2 • Bucket sort according 3 to least significant digit. 4 064 5 216 6 7 008 8 9 47

  70. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 512 2 • Bucket sort according 3 to least significant digit. 4 064 5 216 6 027 7 008 8 9 48

  71. Radix Sort 06 4 00 8 21 6 51 2 02 7 72 9 00 0 00 1 34 3 12 5 0 1 512 2 • Bucket sort according 3 to least significant digit. 4 064 5 216 6 027 7 008 8 9 729 49

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend