Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.

Similar presentations


Presentation on theme: "Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the."— Presentation transcript:

1 Searching Arrays

2 COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the value is found. Otherwise, return -1. * Algorithm: Start with the first array element (index 0) WHILE(more elements in array){ If value found at current index, return index Try next element (increment index) } Value not found, return -1

3 COMP104 Lecture 22 / Slide 3 Unordered Linear Search // Searches an unordered array of integers int search(int data[], // input: array int size, // input: array size int value){ // input: value to find // output: index if found // otherwise return -1 for(int n=0; n<size; n++) if(data[n] == value) return n; return -1; }

4 COMP104 Lecture 22 / Slide 4 Unordered Linear Search void main() { int A[8] = { 10, 7, 9, 1, 17, 30, 5, 6 }; int x; cout << "Enter search element: "; cin >> x; int index = search(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }

5 COMP104 Lecture 22 / Slide 5 Ordered Linear Search * Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1. * The key difference in design is that this array is ordered. * If we perform a linear search, and find that we have already passed where the element might be found, we can quit early.

6 COMP104 Lecture 22 / Slide 6 Ordered Linear Search * not found … n search for -1 n search for 8 n search for 100

7 COMP104 Lecture 22 / Slide 7 Ordered Linear Search * Algorithm: Start with the first array element (index 0) WHILE(more elements in the array){ If value at current index is greater than value, then value not found, return -1 If value found at current index, return index Try next element (increment index) } Value not found, return -1

8 COMP104 Lecture 22 / Slide 8 Ordered Linear Search // Searches an ordered array of integers int lsearch(int data[], // input: array int size, // input: array size int value // input: value to find ) { // output: index if found for(int n=0; n<size; n++){ if(data[n] > value) return -1; else if(data[n] == value) return n; } return -1; }

9 COMP104 Lecture 22 / Slide 9 Ordered Linear Search void main() { int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 }; int x; cout << "Enter search element: "; cin >> x; int index = lsearch(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }

10 COMP104 Lecture 22 / Slide 10 Binary Search * Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1. * Binary search takes advantage of the sorting, to search the array efficiently.

11 COMP104 Lecture 22 / Slide 11 Binary Search * Binary search is based on a divide-and- conquer strategy which works as follows: n Start by looking at the middle element of the array  1. If the middle element is smaller than the search element (e.g. 17), eliminate the first half.  2. If the middle element is larger than the search element (e.g. 6), eliminate the second half. n Repeat this process until the element is found, or until the entire array is eliminated. upper bound lower bound

12 COMP104 Lecture 22 / Slide 12 middle 6<10 Binary Search 24681012141618 Search for 6 lowerupper middle 6=6

13 COMP104 Lecture 22 / Slide 13 middle 12>10 Binary Search 24681012141618 Searching for 14 lowerupper lower middle 12<14 upper middle

14 COMP104 Lecture 22 / Slide 14 Binary Search * Algorithm: Set lower and upper bound of array to be searched Repeat the following: Find the middle element between lower and upper bounds IF ( middle element contains the search value ) return middle element position ELSE IF ( lower bound >= upper bound ) // nothing left to search return -1 ELSE IF ( value < the value of middle element ) set upperbound to middle element position - 1 ELSE set lower bound to middle element position + 1

15 COMP104 Lecture 22 / Slide 15 Binary Search // Searches an ordered array of integers int bsearch(int data[], // input: array int size, // input: array size int value // input: value to find ) { // output: index if found // otherwise return -1 int lower, middle, upper; lower = 0; upper = size - 1; while (true) { middle = (lower + upper) / 2; if (data[middle] == value) return middle; else if (lower >= upper) return -1; else if (value < data[middle]) upper = middle - 1; else lower = middle + 1; }

16 COMP104 Lecture 22 / Slide 16 Binary Search void main() { int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 }; int x; cout << "Enter search element: "; cin >> x; cout << bsearch(A,8,x) << endl; int index = bsearch(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }


Download ppt "Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the."

Similar presentations


Ads by Google