Arrays Continue Sorting and searching outlines Sorting Bubble
Arrays (Continue) Sorting and searching
outlines • Sorting – Bubble sort • Linear search – min and max • Binary search
Sort Bubble Sort
Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 42 77 1 5 3 2 5 4 35 6 101 12 35 3 12 4 5 5 6 42 77 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 pairwise comparisons and swapping 1 2 7742 Swap 4277 3 4 35 5 6 12 101 5
"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 pairwise comparisons and swapping 1 42 2 3 4 7735 Swap 3577 5 6 12 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 5 77 7712 Swap 12 6 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 101 No need to swap 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 1015 Swap 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 Largest value correctly placed 5 101
The “Bubble Up” Algorithm for(int i = 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 42 2 3 35 4 12 5 6 77 Largest value correctly placed 5 101
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.
“Bubbling” All the Elements 1 2 42 1 35 2 35 N-1 1 5 3 2 5 5 4 4 4 35 5 101 77 101 6 5 5 6 42 35 3 12 4 6 77 5 3 2 5 42 35 12 1 3 2 4 12 12 12 1 3 5 6 42
Reducing the Number of Comparisons 1 2 77 1 42 2 42 1 12 3 2 35 5 4 4 4 35 101 5 5 101 77 101 6 77 5 6 5 5 6 42 5 3 5 4 6 12 42 3 2 5 12 12 12 1 3 2 4 35 35 35 1 3 5 6 42
for(int i = 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; } } }
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”! 1 5 2 3 12 4 35 5 6 42 77 101
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(int i = 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 j true swap 0 index 98 1 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. lenght 8 to_do 0 index 1 98 1 false did_swap 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 0 index 1 false did_swap Swap 98 1 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 0 index 1 true did_swap Swap 23 1 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. length 8 j 7 index 2 23 1 true did_swap 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 2 true did_swap Swap 23 1 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. length 8 j 0 index 2 true did_swap Swap 23 1 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. length 8 j 0 index 3 23 1 true swap 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. lengt 8 j 0 index 3 true swap Swap 23 1 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. lengt 8 j 0 index 3 true swap Swap 23 1 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. lengt 8 j 0 index 4 23 1 true swap 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. length 8 j 0 index 4 true did_swap Swap 23 1 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example A. length 8 j 0 index 4 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example A. lengt 8 j 0 index 5 23 1 true swap 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example A. lengt 8 j 0 index 5 true swap Swap 23 1 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example A. length 8 j 0 index 5 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example A. length 8 j 0 index 6 23 1 true swap 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example A. lengt 8 j 0 index 6 true swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example A. lengt 8 j 0 index 6 true swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example A. lengt 8 j 0 index 7 23 1 true swap 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example A. length 8 j 0 index 7 true swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example A. length 8 j 0 index 7 true swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
After First Pass of Outer Loop A. length 8 j 0 index 8 23 1 true swap Finished first “Bubble Up” 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 2 index 1 23 1 false swap 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 2 index 1 false swap No Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 2 23 1 false swap 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 2 false did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 2 true did_swap Swap 23 1 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 3 23 1 true swap 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 3 true did_swap Swap 23 1 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 3 true swap Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 4 23 1 true did_swap 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 1 index 4 true swap No Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 1 index 5 23 1 true swap 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 1 index 5 true swap Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 5 true swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” A. length 8 j 1 index 6 23 1 true swap 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” N 8 to_do 1 index 6 true swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” N 8 to_do 1 index 6 true swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 42 8 67 98
After Second Pass of Outer Loop N 8 true swap 1 index Finished second “Bubble Up” 7 23 1 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 1 23 1 false swap 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 1 false swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 1 true swap Swap 14 1 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 2 14 1 true swap 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. lengt 8 j 2 index 2 true swap Swap 14 1 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. lengt 8 j 2 index 2 true swap Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 2 index 3 14 1 true did_swap 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 3 true swap No Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 4 14 1 true did_swap 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 4 true swap Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 4 true swap Swap 14 1 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 5 14 1 true did_swap 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 5 true swap Swap 14 1 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” A. length 8 j 2 index 5 true did_swap Swap 14 1 6 23 33 2 3 4 5 6 42 7 45 8 67 98
After Third Pass of Outer Loop A. length 8 j 2 index 6 14 1 true did_swap Finished third “Bubble Up” 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 1 14 1 false swap 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 1 false swap Swap 14 1 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 3 index 1 true did_swap Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 2 6 1 true swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 2 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 3 6 1 true swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 3 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 4 6 1 true swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” A. length 8 j 3 index 4 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
After Fourth Pass of Outer Loop A. length 8 j 3 index 5 6 1 true swap Finished fourth “Bubble Up” 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” A. length 8 j 4 index 1 6 1 false swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” A. lenght 8 j 4 index 1 false swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” A. length 8 j 4 index 2 6 1 false swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” A. length 8 j 4 index 2 false swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” A. length 8 j 4 index 3 6 1 false swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
Finished “Early” A. lengt 8 j 3 index 4 false swap We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
Bubble sort Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
Linear search Use linear to seach and value is contained in one array.
Searching for x in an array a for(int i = 0; i < a. length; ++ i) { if (a[i] == x) System. out. println(“true”); }
Finding the minimal value in array int min. Value = a[0]; int min. Index = 0; for(int i = start + 1; i < end; i ++) { if (a[i] < min. Value) { min. Value = a[i]; min. Index = i; }
Finding the minimal value in array int max. Value = a[0]; int max. Index = 0; for(int i = start + 1; i < end; i ++) { if (a[i] > max. Value) { max. Value = a[i]; max. Index = i; }
Binary search for a value in array Searching if x is in array a. We assume a is sorted in ascending order. int start = 0; int end = a. length - 1; boulean found = flase while (start <= end) { int m = (start + end)/2; if (x == a[m]) {found =false; } else if (x < a[m]) { end = m - 1; } else { start = m + 1; } }
summary • Buble sort • Learnear search • Binary sort
- Slides: 97