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
"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 4277
"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 3577
"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 1277
"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 No need to swap
"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 5101
"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 Largest value correctly placed
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 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 well correctly place all N elements.
Bubbling All the Elements N - 1
Reducing the Number of Comparisons
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!
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 j index 0 A.length 8 swap true
An Animated Example to_do index 0 1 A.lenght 8 did_swap false
An Animated Example to_do index 0 1 N 8 Swap did_swap false
An Animated Example to_do index 0 1 N 8 Swap did_swap true
An Animated Example j index 7 2 A.length 8 did_swap true
An Animated Example to_do index 7 2 N 8 Swap did_swap true
An Animated Example j index 0 2 A.length 8 Swap did_swap true
An Animated Example j index 0 3 A.length 8 swap true
An Animated Example j index 0 3 A.lengt 8 Swap swap true
An Animated Example j index 0 3 A.lengt 8 Swap swap true
An Animated Example j index 0 4 A.lengt 8 swap true
An Animated Example j index 0 4 A.length 8 Swap did_swap true
An Animated Example j index 0 4 A.length 8 Swap did_swap true
An Animated Example j index 0 5 A.lengt 8 swap true
An Animated Example j index 0 5 A.lengt 8 Swap swap true
An Animated Example j index 0 5 A.length 8 Swap did_swap true
An Animated Example j index 0 6 A.length 8 swap true
An Animated Example j index 0 6 A.lengt 8 Swap swap true
An Animated Example j index 0 6 A.lengt 8 Swap swap true
An Animated Example j index 0 7 A.lengt 8 swap true
An Animated Example j index 0 7 A.length 8 Swap swap true
An Animated Example j index 0 7 A.length 8 Swap swap true
After First Pass of Outer Loop j index 0 8 A.length 8 Finished first Bubble Up swap true
The Second Bubble Up j index 2 1 A.length 8 swap false
The Second Bubble Up j index 2 1 A.length 8 swap false No Swap
The Second Bubble Up j index 1 2 A.length 8 swap false
The Second Bubble Up j index 1 2 A.length 8 did_swap false Swap
The Second Bubble Up j index 1 2 A.length 8 did_swap true Swap
The Second Bubble Up j index 1 3 A.length 8 swap true
The Second Bubble Up j index 1 3 A.length 8 did_swap true Swap
The Second Bubble Up j index 1 3 A.length 8 swap true Swap
The Second Bubble Up j index 1 4 A.length 8 did_swap true
The Second Bubble Up to_do index 1 4 N 8 swap true No Swap
The Second Bubble Up to_do index 1 5 N 8 swap true
The Second Bubble Up to_do index 1 5 N 8 swap true Swap
The Second Bubble Up j index 1 5 A.length 8 swap true Swap
The Second Bubble Up j index 1 6 A.length 8 swap true
The Second Bubble Up to_do index 1 6 N 8 swap true Swap
The Second Bubble Up to_do index 1 6 N 8 swap true Swap
After Second Pass of Outer Loop index 1 7 N 8 swap true Finished second Bubble Up
The Third Bubble Up j index 2 1 A.length 8 swap false
The Third Bubble Up j index 2 1 A.length 8 swap false Swap
The Third Bubble Up j index 2 1 A.length 8 swap true Swap
The Third Bubble Up j index 2 2 A.length 8 swap true
The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap
The Third Bubble Up j index 2 2 A.lengt 8 swap true Swap
The Third Bubble Up to_do index 2 3 N 8 did_swap true
The Third Bubble Up j index 2 3 A.length 8 swap true No Swap
The Third Bubble Up j index 2 4 A.length 8 did_swap true
The Third Bubble Up j index 2 4 A.length 8 swap true Swap
The Third Bubble Up j index 2 4 A.length 8 swap true Swap
The Third Bubble Up j index 2 5 A.length 8 did_swap true
The Third Bubble Up j index 2 5 A.length 8 swap true Swap
The Third Bubble Up j index 2 5 A.length 8 did_swap true Swap
After Third Pass of Outer Loop j index 2 6 A.length 8 did_swap true Finished third Bubble Up
The Fourth Bubble Up j index 3 1 A.length 8 swap false
The Fourth Bubble Up j index 3 1 A.length 8 swap false Swap
The Fourth Bubble Up to_do index 3 1 N 8 did_swap true Swap
The Fourth Bubble Up j index 3 2 A.length 8 swap true
The Fourth Bubble Up j index 3 2 A.length 8 did_swap true No Swap
The Fourth Bubble Up j index 3 3 A.length 8 swap true
The Fourth Bubble Up j index 3 3 A.length 8 did_swap true No Swap
The Fourth Bubble Up j index 3 4 A.length 8 swap true
The Fourth Bubble Up j index 3 4 A.length 8 did_swap true No Swap
After Fourth Pass of Outer Loop j index 3 5 A.length 8 swap true Finished fourth Bubble Up
The Fifth Bubble Up j index 4 1 A.length 8 swap false
The Fifth Bubble Up j index 4 1 A.lenght 8 swap false No Swap
The Fifth Bubble Up j index 4 2 A.length 8 swap false
The Fifth Bubble Up j index 4 2 A.length 8 swap false No Swap
The Fifth Bubble Up j index 4 3 A.length 8 swap false
Finished Early j index 3 4 A.lengt 8 swap false We didnt do any swapping, so all of the other elements must be correctly placed. We can skip the last two passes of the outer loop.
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 minValue = a[0]; int minIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] < minValue) { minValue = a[i]; minIndex = i; }
Finding the minimal value in array int maxValue = a[0]; int maxIndex = 0; for(int i = start + 1; i < end; i ++) { if (a[i] > maxValue) { maxValue = a[i]; maxIndex = 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