/* testsearch2.cpp CIS 150 David Klick 6/28/05 Demonstration of binary search. */ #include #include #include using std::cout; const int ARRAY_SIZE = 10; int bsearch(int n[], int len, int key); void sort_array(int n[], int len); int main(void) { int nums[ARRAY_SIZE]; int i, val, pos; // randomize random number generator srand(time(NULL)); // fill array up with random ints (0-99) for (i=0; i key) hi = mid - 1; else if (n[mid] < key) lo = mid + 1; else return mid; } return -1; } // sorts array n[] into ascending order void sort_array(int n[], int len) { int i, j, tmp; for (i=0; i n[j]) { tmp = n[i]; n[i] = n[j]; n[j] = tmp; } } } } /* Sample output 13 15 27 33 36 50 51 54 89 90 1 not found 15 found at position 1 47 not found 95 not found 40 not found 60 not found 54 found at position 7 55 not found 78 not found 66 not found */