280 likes | 370 Views
Principles of Computer Science I Honors Section. Note Set 5 CSE 1341. Today:. Searching and Sorting Arrays. Searching. Looking for a particular value in an array Linear Search Start at the beginning (or end) and iteratively check every element Until you find it
E N D
Principles of Computer Science IHonors Section Note Set 5 CSE 1341
Today: Searching and Sorting Arrays
Searching • Looking for a particular value in an array • Linear Search • Start at the beginning (or end) and iteratively check every element • Until you find it • OR until you reach the end • Binary Search • Start in the middle of a sorted array • Eliminates ½ of the remaining search space on each iteration
Linear Search int test[5]={10, 42, 8, 100, -3}; int result; result = searchArray(test, 5, 100); if (result == -1) cout << “100 Not Found” << endl; else cout << “100 Found” << endl; //Returns Subscript of element or -1 if not found int searchArray(int arr[], int numElem, int target) { for(int x = 0; x < numElem; x++) { if (arr[x] == target) return x; } return (-1); }
Searching • Also Searching • Finding the largest or smallest element in an array //Returns Subscript of largest element int //Largest value placed in val searchArray(int arr[], int numElem, int& val) { val = arr[0]; int subs = 0; for(int x = 1; x < numElem; x++) { if (arr[x] > val) { val = arr[x]; subs = x; } } return subs; }
Binary Search • Array must be sorted • Only searching ½ of the array in each iteration of the search int main() { int test[13]={1,5,7,8,10,11,14,16,22, 35,44,57,66}; int result = binarySearch(test,13,16); if(result == -1) cout<<“Number not found”<<endl; else cout<<“Number found”<<endl; return 0; }
Binary Search - Intuition 16 value 1 5 7 8 10 11 14 16 22 35 44 57 66 [0] [6] [12] We ask:Is Value in the top half of the array, or the bottom half? Determine this based on comparing value to middle element
Binary Search - Implementation int binarySearch(int arr[], int numElem, int val) { int first = 0, last = numElem – 1, middle; while(first <= last) { middle=first+(last-first)/2; if (arr[middle]==val) return middle; else if(arr[middle]>val) last = middle – 1; else first = middle + 1; } return -1; } find the middle index If val is in bottom half If val is in top half
Binary Search - Intuition Iter 1 16 val 1 5 7 8 10 11 14 16 22 35 44 57 66 [0] [6] [12] last first middle = first+(last-first)/2 = 6 arr[middle] = 14 Is val >, < or == 14 > so: first = middle + 1 first = 7
Binary Search - Intuition Iter 2 first = 7 last = 12 16 val 1 5 7 8 10 11 14 16 22 35 44 57 66 [7] [9] [0] [6] [12] first last middle = first+(last-first)/2 = 9 arr[middle] = 35 Is val >, < or == 35 < so: last = middle - 1 last = 8
Binary Search - Intuition Iter 3 first = 7 last = 8 16 val 1 5 7 8 10 11 14 16 22 35 44 57 66 [7] [8] [9] [0] [6] [12] last first middle = first+(last-first)/2 = 7 arr[middle] = 16 Is val >, < or == 16 = so: return middle return 7
Motivation • Searching sorted data is faster than searching unsorted data • What if the phonebook wasn’t in alphabetical order? • Sorted data is usually easier to deal with • Can sort ascending or descending
5 3 1 2 swap if out of order 3 5 1 2 swap if out of order 3 1 5 2 3 1 2 5 1 3 2 5 1 2 3 5 Bubble Sort Intuition …and so on until you make one pass without swapping
Bubble Sort – Swap Function void swap(int& x, int& y) { int temp = x; x = y; y = temp; }
Bubble Sort void bubbleSort(int arr[], int numElem) { bool swapped; do { swapped = false; for (int x = 0; x < (numElem-1); x++) { if (arr[x] > arr[x+1]) { swap(arr[x], arr[x+1]); swapped = true; } } } while (swapped); }
Bubble Sort - Example true x=0 swapped = false 2 7 2 7 2 7 3 8 9 1 arr [0] [1] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; }
Bubble Sort - Example x=1 swapped = true 2 7 3 3 7 8 9 1 arr [1] [2] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; }
Bubble Sort - Example x=2 swapped = true 2 3 7 8 9 1 arr [2] [3] [5] false – do nothing if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; }
Bubble Sort - Example x=3 swapped = true 2 3 7 8 9 1 arr [3] [4] [5] false – do nothing if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; }
Bubble Sort - Example x=4 swapped = true 2 3 7 8 9 1 1 9 arr [4] [5] if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true; } Finished w/ 1st Iteration of do-while loop. . .
Bubble Sort - Example true swapped = false 2 3 7 8 1 8 1 9 arr [0] [1] [2] [3] [4] [5] Finished w/ 2nd Iteration of do-while loop. . .
Bubble Sort - Example true swapped = false 2 3 1 7 7 1 8 9 arr [0] [1] [2] [3] [4] [5] Finished w/ 3rd Iteration of do-while loop. . .
Bubble Sort - Example true swapped = false 2 1 3 1 3 7 8 9 arr [0] [1] [2] [3] [4] [5] Finished w/ 4th Iteration of do-while loop. . .
Bubble Sort - Example true swapped = false 1 2 2 1 3 7 8 9 arr [0] [1] [2] [3] [4] [5] Finished w/ 5th Iteration of do-while loop. . . but still must go through one more time!
Bubble Sort - Example swapped = false 1 2 3 7 8 9 arr [0] [1] [2] [3] [4] [5] DONE!