#include "searchArray.h" // compare key to every element of array until location is // found or until end of array is reached; return subscript of // element if key or -1 if key not found int linearSearch( const int array[], int key, int sizeOfArray ) { for ( int j = 0; j < sizeOfArray; j++ ) if ( array[ j ] == key ) // if found, return j; // return location of key return -1; // key not found } // end function linearSearch // function to perform binary search of an array int binarySearch( const int b[], int searchKey, int low, int high, int size ) { int middle; // loop until low subscript is greater than high subscript while ( low <= high ) { // determine middle element of subarray being searched middle = ( low + high ) / 2; // if searchKey matches middle element, return middle if ( searchKey == b[ middle ] ) // match return middle; else // if searchKey less than middle element, // set new high element if ( searchKey < b[ middle ] ) high = middle - 1; // search low end of array // if searchKey greater than middle element, // set new low element else low = middle + 1; // search high end of array } return -1; // searchKey not found } // end function binarySearch