data structures in java
play

Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel - PowerPoint PPT Presentation

Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1 Sorting Midterm Exams 2 Sorting Input: 34 8 64 51 32 21 Array containing unordered Comparables (duplicates allowed). Output: 8 21 32 34 51 64


  1. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 64 51 32 34 8 30 1 2 5 h 3 = 5 h 2 = 3 h 1 = 1 9

  2. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 64 51 32 34 8 30 1 2 5 h 3 = 5 h 2 = 3 h 1 = 1 10

  3. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 51 32 34 8 64 1 2 5 h 3 = 5 h 2 = 3 h 1 = 1 10

  4. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 51 32 34 8 64 1 2 5 h 3 = 5 h 2 = 3 h 1 = 1 11

  5. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 1 32 34 8 64 51 2 5 h 3 = 5 h 2 = 3 h 1 = 1 11

  6. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 1 32 34 8 64 51 2 5 h 3 = 5 h 2 = 3 h 1 = 1 12

  7. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 1 2 34 8 64 51 32 5 h 3 = 5 h 2 = 3 h 1 = 1 12

  8. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 1 2 34 8 64 51 32 5 h 3 = 5 h 2 = 3 h 1 = 1 13

  9. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 21 7 30 1 2 5 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 13

  10. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 5 7 30 1 2 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 13

  11. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 5 7 30 1 2 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 14

  12. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 7 30 5 2 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 14

  13. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 7 30 5 2 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 15

  14. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 30 5 7 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 15

  15. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 30 5 7 21 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 16

  16. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 16

  17. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 17

  18. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 18

  19. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 19

  20. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 20

  21. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 64 51 32 34 h 3 = 5 h 2 = 3 h 1 = 1 21

  22. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 21

  23. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 22

  24. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 23

  25. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 21 5 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 24

  26. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 5 1 2 21 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 24

  27. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 21 7 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 25

  28. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 21 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 25

  29. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 21 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 26

  30. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 21 30 8 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 27

  31. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 21 8 30 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 27

  32. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 27

  33. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 28

  34. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 29

  35. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 34 51 32 64 h 3 = 5 h 2 = 3 h 1 = 1 30

  36. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 34 32 51 64 h 3 = 5 h 2 = 3 h 1 = 1 30

  37. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 32 34 51 64 h 3 = 5 h 2 = 3 h 1 = 1 30

  38. Shell Sort • Generalize insertion sort so that items that are further apart can be swapped. • Break up sorting into phases. Each phase k makes sure that all items space h k apart are sorted. • “increment sequence” of steps h 1 , h 2 , … ,h t 1 2 5 7 8 21 30 32 34 51 64 h 3 = 5 h 2 = 3 h 1 = 1 31

  39. Shell Sort • The running time analysis for shell sort is complex and depends on the specific increment sequence. • With Hibbard’s sequence (1,3,7,15,…,2 k -1) 
 worst case running time is 32

  40. Sorting Stability • Assume we put key/value pairs sorted by keys into the 
 array. • Shell Sort is unstable : keys will be sorted, but values 
 for the same key may be in different order than in the 
 input. 34 8 64 1 1 4 3 30 7 2 5 val1 val2 33

  41. Sorting Stability • Assume we put key/value pairs sorted by keys into the 
 array. • Shell Sort is unstable : keys will be sorted, but values 
 for the same key may be in different order than in the 
 input. 1 8 64 1 34 4 3 30 7 2 5 val2 val1 33

  42. Space Requirements • Both Insertion Sort and Shell Sort operate in place. • Only a small amount of memory required to store a 
 temporary value for swaps. • Space requirement: O(1) 34

  43. Heap Sort • First convert an unordered array into a heap in O(N) time. • Then perform N deleteMin operations to retrieve the elements in sorted order. • each deleteMin is O(log N) 35

  44. Heap Sort • First convert an unordered array into a heap in O(N) time. • Then perform N deleteMin operations to retrieve the elements in sorted order. • each deleteMin is O(log N) • Problem: This algorithm requires a second array to 
 store the output: O(N) space! 
 • Idea: re-use the freed space after each deleteMin. 35

  45. Heap Sort Example 5 4 6 9 1 8 3 10 7 2 11 36

  46. Heap Sort Example 5 4 6 9 1 8 3 10 11 7 2 5 4 6 9 1 8 3 10 7 2 11 36

  47. Heap Sort Example Build heap in O(N) time 1 2 3 4 7 8 6 10 11 5 9 1 2 3 7 4 8 6 10 9 5 11 37

  48. Heap Sort Example Build heap in O(N) time 1 2 3 4 7 8 6 10 11 5 9 1 2 3 7 4 8 6 10 9 5 11 37

  49. Heap Sort Example deleteMin, write min element into empty cell 1 2 3 4 7 8 6 10 11 5 9 1 11 2 3 7 4 8 6 10 9 5 38

  50. Heap Sort Example deleteMin, write min element into empty cell 11 2 3 4 7 8 6 10 5 9 11 2 3 7 4 8 6 10 9 5 1 38

  51. Heap Sort Example Percolate down 2 4 3 5 7 8 6 10 11 9 1 2 4 3 7 5 8 6 10 9 11 39

  52. Heap Sort Example deleteMin, write min element into empty cell 2 4 3 5 7 8 6 10 11 9 1 2 11 4 3 7 5 8 6 10 9 40

  53. Heap Sort Example deleteMin, write min element into empty cell 11 4 3 5 7 8 6 10 9 11 1 2 4 3 7 5 8 6 10 9 40

  54. Heap Sort Example Percolate down 3 4 6 5 7 8 11 10 9 1 3 2 4 6 7 5 8 11 10 9 41

  55. Heap Sort Example deleteMin, write min element into empty cell 4 5 6 7 9 8 11 10 1 4 2 5 6 7 9 8 11 10 3 42

  56. Heap Sort Example deleteMin, write min element into empty cell 5 7 6 10 9 8 11 1 5 2 7 6 10 9 8 11 4 3 43

  57. Heap Sort Example deleteMin, write min element into empty cell 6 7 8 10 9 11 1 6 2 7 8 10 9 11 5 4 3 44

  58. Heap Sort Example deleteMin, write min element into empty cell 7 9 8 10 11 1 7 2 9 8 10 11 6 5 4 3 45

  59. Heap Sort Example deleteMin, write min element into empty cell 8 9 11 10 1 8 2 9 11 10 7 6 5 4 3 46

  60. Heap Sort Example deleteMin, write min element into empty cell 9 10 11 1 9 2 10 11 8 7 6 5 4 3 47

  61. Heap Sort Example deleteMin, write min element into empty cell 10 11 1 10 2 11 9 8 7 6 5 4 3 48

  62. Heap Sort Example 11 • Can use a max-heap if we want the output in 
 increasing order. 1 11 2 10 9 8 7 6 5 4 3 49

  63. Merge Sort • A classic divide-and-conquer algorithm. • Split the array in half, recursively sort each half. • Merge the two sorted lists. 34 8 64 51 32 21 1 2 50

  64. Merge Sort • A classic divide-and-conquer algorithm. • Split the array in half, recursively sort each half. • Merge the two sorted lists. 34 8 64 51 21 1 2 32 50

  65. Merge Sort • A classic divide-and-conquer algorithm. • Split the array in half, recursively sort each half. • Merge the two sorted lists. 2 8 34 64 1 21 32 51 51

  66. Merge Sort • A classic divide-and-conquer algorithm. • Split the array in half, recursively sort each half. • Merge the two sorted lists. 2 8 34 64 1 21 32 51 8 21 32 34 51 64 1 2 51

  67. Merging Sorted Sublists Keep a pointers for each sub-list in the array. • In each step, compare the elements they point two. • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr. • Otherwise, copy a[Bctr] to the output and advance Bctr. • a 2 8 34 64 1 21 32 51 Actr Bctr tmp 52 Cctr

  68. Merging Sorted Sublists Keep a pointers for each sub-list in the array. • In each step, compare the elements they point two. • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr. • Otherwise, copy a[Bctr] to the output and advance Bctr. • a 2 8 34 64 1 21 32 51 Actr Bctr 1 tmp 52 Cctr

Recommend


More recommend