Question: Question (c++) Binary Search Write a recursive function that conducts a binary search with the following interface: bool BinarySearch(int info[ ], int x, int fromLoc,

Question (c++) Binary Search

Write a recursive function that conducts a binary search with the following interface:

bool BinarySearch(int info[ ], int x, int fromLoc, int toLoc, int &level)

{

}

Input:

info[ ] ---- an integer array that contains a list of numbers

x ---- the search item

fromLoc, toLoc --- the beginning and ending indices of the array elements that represent the bounds of a list.

level --- how many levels of recursion used in the execution.

Output:

True if x is in the list; false, otherwise.

In your main( ):

int main( )

{

int info[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int level = 0, x = 11;

bool flag;

flag = BinarySearch(info, x, 0, 9, level);

cout << Position 1: << flag << << level = << level << endl;

x = 8; level = 0;

flag = BinarySearch(info, x, 0, 9, level);

cout << Position 2: << flag << << level = << level << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!