// file: binsearch.cpp // this is the binary search function // it takes an array, a size, and a key as parameters // it returns the location of the key if found, returns // -1 if not found int binSearch(int ary[], int size, int key) { int found = 0; int mid, first, last; first = 0; last = size - 1; while (first <= last && found == 0) { mid = (first + last)/2; if (key == ary[mid]) // found the location found = 1; else if (key < ary[mid]) // search the first half last = mid - 1; else first = mid + 1; // search the second half } // while if (found == 0) // not found mid = -1; return mid; }