Searching Arrays ANSIC Searching Searching looking for something

  • Slides: 8
Download presentation
Searching Arrays ANSI-C

Searching Arrays ANSI-C

Searching • Searching = looking for something • Searching an array is particularly common

Searching • Searching = looking for something • Searching an array is particularly common • Goal: determine if a particular value is in the array • If the entries in the array are not in order: • start at the beginning and look at each element to see if it matches

Linear Search /* If x appears in table[0. . length-1], return its * position

Linear Search /* If x appears in table[0. . length-1], return its * position in the table, i. e. , * return k so that table[k]==entry. If entry not * found, return -1 */ int lsearch (int table[ ], int length, int entry) { int index = 0; while (index < length && table[index] != entry) { index = index + 1; } if (index >= length ) index = -1; return index; }

Binary search • Can we do better? • "Binary search" works if the array

Binary search • Can we do better? • "Binary search" works if the array is sorted • The strategy in this case is: – 1. Look for the target in the middle. – 2. If you don’t find it, you can ignore half of the array, and repeat the process with the other half. • Example: Find first page of Pizza • listings in the yellow pages

Binary Search /* If entry appears in table[0. . length-1], return its * location,

Binary Search /* If entry appears in table[0. . length-1], return its * location, i. e. return k so that table[k]==entry. * If entry not found, return -1 */ int bsearch (int table[ ], int length, int entry) { int left; int right; int middle; while ( ________ ) { middle = (left + right) / 2; if (table[middle] < entry) left = middle + 1; else right = middle - 1; } _________ ; }

Binary Search /* If entry appears in table[0. . length-1], return its * location,

Binary Search /* If entry appears in table[0. . length-1], return its * location, i. e. return k so that table[k]==entry. * If entry not found, return -1 */ int bsearch (int table[ ], int length, int entry) { int left = 0; int right = length – 1; int middle; while ( left <= right) { middle = (left + right) / 2; if (table[middle] < entry) left = middle + 1 else right = middle - 1; } _________ ; }

Binary Search /* If entry appears in table[0. . length-1], return its * location,

Binary Search /* If entry appears in table[0. . length-1], return its * location, i. e. return k so that table[k]==entry. * If entry not found, return -1 */ int bsearch (int table[ ], int length, int entry) { int left = 0; int right = length – 1; int middle; int k = -1; while ( left <= right) { middle = (left + right) / 2; if (table[middle] < entry) left = middle + 1; else right = middle - 1; } if ( left < length && table[left] == entry ) k = left; return k; }

Is it worth the trouble? • Suppose you had 1000 elements • Linear search

Is it worth the trouble? • Suppose you had 1000 elements • Linear search would require maybe 500 comparisons on average • Binary search – after 1 st compare, throw away half, leaving 500 elements to be searched. – after 2 nd compare, throw away half, leaving 250. – Then 125, 63, 32, 16, 8, 4, 2, 1 are left. – After at most 10 steps, you’re done! • What if you had 1, 000 elements? ?