1 / 97

Arrays (Continue)

Arrays (Continue). Sorting and searching. outlines. Sorting Bubble sort Linear search min and max Binary search. Sort. Bubble Sort. 35. 12. 77. 101. 5. 42. Sorting. Sorting takes an unordered collection and makes it an ordered one.

garton
Download Presentation

Arrays (Continue)

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. Arrays (Continue) Sorting and searching

  2. outlines • Sorting • Bubble sort • Linear search • min and max • Binary search

  3. Sort Bubble Sort

  4. 35 12 77 101 5 42 Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 101 12 42 35 5 77 1 2 3 4 5 6

  5. 42 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 1 2 3 4 5 6 101 12 42 35 5 77

  6. 35 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 77 35 5 42

  7. 12 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 35 77 5 42

  8. "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 77 35 12 5 42 No need to swap

  9. 5 101 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 77 35 12 5 42

  10. "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed

  11. The “Bubble Up” Algorithm for(inti = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; } } }

  12. Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed

  13. Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.

  14. 1 2 3 4 5 6 77 5 101 42 35 12 1 2 3 4 5 6 5 77 101 35 12 42 1 2 3 4 5 6 42 77 101 12 35 5 N - 1 1 2 3 4 5 6 42 77 101 12 5 35 1 2 3 4 5 6 42 77 101 5 12 35 “Bubbling” All the Elements

  15. 1 2 3 4 5 6 77 5 101 42 35 12 1 2 3 4 5 6 5 77 101 35 12 42 1 2 3 4 5 6 42 77 101 12 35 5 1 2 3 4 5 6 42 77 101 12 5 35 1 2 3 4 5 6 12 101 5 77 42 35 Reducing the Number of Comparisons

  16. for(inti = 0; i<A.length; i++) { for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; } } }

  17. 1 2 3 4 5 6 42 77 101 5 12 35 Already Sorted Collections? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? • We want to be able to detect this and “stop early”!

  18. Using a Boolean “Flag” • We can use a boolean variable to determine if any swapping occurred during the “bubble up.” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up.”

  19. Add flag to reduce the number of iterations Boolean swap = false; for(inti = 0; i<A.length && !swap; i++) { boolean swap = false; for(int j = 0; j < A.length-1; j++) { if(A[j]>A[j+1]) { temp= A[j]; A[j]=A[j+1]; A[j+1]=temp; swap = true; } } }

  20. An Animated Example A.length 8 true swap j 0 index 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  21. An Animated Example A.lenght 8 false did_swap to_do 0 index 1 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  22. An Animated Example N 8 false did_swap to_do 0 index 1 Swap 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  23. An Animated Example N 8 true did_swap to_do 0 index 1 Swap 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  24. An Animated Example A.length 8 true did_swap j 7 index 2 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  25. An Animated Example N 8 true did_swap to_do 7 index 2 Swap 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8

  26. An Animated Example A.length 8 true did_swap j 0 index 2 Swap 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8

  27. An Animated Example A.length 8 true swap j 0 index 3 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8

  28. An Animated Example A.lengt 8 true swap j 0 index 3 Swap 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8

  29. An Animated Example A.lengt 8 true swap j 0 index 3 Swap 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8

  30. An Animated Example A.lengt 8 true swap j 0 index 4 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8

  31. An Animated Example A.length 8 true did_swap j 0 index 4 Swap 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8

  32. An Animated Example A.length 8 true did_swap j 0 index 4 Swap 23 45 14 6 98 67 33 42 1 2 3 4 5 6 7 8

  33. An Animated Example A.lengt 8 true swap j 0 index 5 23 45 14 6 98 67 33 42 1 2 3 4 5 6 7 8

  34. An Animated Example A.lengt 8 true swap j 0 index 5 Swap 23 45 14 6 98 67 33 42 1 2 3 4 5 6 7 8

  35. An Animated Example A.length 8 true did_swap j 0 index 5 Swap 23 45 14 6 67 98 33 42 1 2 3 4 5 6 7 8

  36. An Animated Example A.length 8 true swap j 0 index 6 23 45 14 6 67 98 33 42 1 2 3 4 5 6 7 8

  37. An Animated Example A.lengt 8 true swap j 0 index 6 Swap 23 45 14 6 67 98 33 42 1 2 3 4 5 6 7 8

  38. An Animated Example A.lengt 8 true swap j 0 index 6 Swap 23 45 14 6 67 33 98 42 1 2 3 4 5 6 7 8

  39. An Animated Example A.lengt 8 true swap j 0 index 7 23 45 14 6 67 33 98 42 1 2 3 4 5 6 7 8

  40. An Animated Example A.length 8 true swap j 0 index 7 Swap 23 45 14 6 67 33 98 42 1 2 3 4 5 6 7 8

  41. An Animated Example A.length 8 true swap j 0 index 7 Swap 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  42. After First Pass of Outer Loop A.length 8 true swap j 0 Finished first “Bubble Up” index 8 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  43. The Second “Bubble Up” A.length 8 false swap j 2 index 1 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  44. The Second “Bubble Up” A.length 8 false swap j 2 index 1 No Swap 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  45. The Second “Bubble Up” A.length 8 false swap j 1 index 2 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  46. The Second “Bubble Up” A.length 8 false did_swap j 1 index 2 Swap 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

  47. The Second “Bubble Up” A.length 8 true did_swap j 1 index 2 Swap 23 14 45 6 67 33 42 98 1 2 3 4 5 6 7 8

  48. The Second “Bubble Up” A.length 8 true swap j 1 index 3 23 14 45 6 67 33 42 98 1 2 3 4 5 6 7 8

  49. The Second “Bubble Up” A.length 8 true did_swap j 1 index 3 Swap 23 14 45 6 67 33 42 98 1 2 3 4 5 6 7 8

  50. The Second “Bubble Up” A.length 8 true swap j 1 index 3 Swap 23 14 6 45 67 33 42 98 1 2 3 4 5 6 7 8

More Related