Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

int binarySearchlconst int 1I, iets,int key) int neint) Karami Siot const int ST

ID: 3709157 • Letter: I

Question



int binarySearchlconst int 1I, iets,int key) int neint) Karami Siot const int STZE18 int artsizE)-,,2, 3, 4 int key cost e kinter key: Replace the 7 return /This function retures the index where key is found or-1 if key is net found. key is the nuber being searched far int binarySearchlconst int atl, ist,int key int 1-???,; //left boudary starts at first ?sd" int rY igt boundary starts at last index int s iddle pelnt betesen 1eft and right. 1 , rss svar, arch ndt //calculate the iddle point beteeen 1 and r irtm? m T7) //key u.. ,eusd return 717777 22P else 3r n7722) return 777ay wasn found

Explanation / Answer

int binarySearch(const int a[],int s,int key);

int main()

{

const int SIZE = 10;

int ar[SIZE] = {1, 10, 20, 30, 40, 50, 60, 70, 80, 90};

int key;

cout << "Enter key: ";

cin >> key;

cout << key << " was found at " << binarySearch(ar,SIZE,key) << endl;

return 0;

}

int binarySearch(const int a[],int s,int key)

{

int l = 0;

int r = l + s - 1;

int m;

while(r >= l)

{

m = l + (r - l)/2;

if(a[m] == key)

return m;

else if(a[m] > x)

r = m - 1;

else if(a[m] < x)

l = m + 1;

}

return -1;

}