1.01k likes | 1.06k Views
Learn about array sorting techniques, including Bubble Sort and Linear Search, to efficiently organize and search through your data. Understand the algorithms and implementation methods.
E N D
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. 1 2 3 4 5 6 101 12 42 35 5 77 1 2 3 4 5 6
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
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
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
"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
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
"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
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; } } }
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
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.
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
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
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; } } }
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”!
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.”
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; } } }
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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