Below is a recursive version of binary search: int binarySearch(int nums[], int low, int high, int target) { if (low > high) return -1; int mid = (low + high)/2; if (target == nums[mid]) return mid; else if (target < nums[mid]) return binarySearch(nums, low, mid - 1, target); else return binarySearch(nums, mid + 1, high, target); } For the 4 statements below, indicate whether you think it is True or False. If you like, you can provide a description of your answer for partial credit in case you are incorrect. 1) The code “if (low > high) return -1;” is a base case for this function 2) The code “int mid = (low + high)/2;” is a base case for this function 3) The code “if (target == nums[mid]) return mid;” is a base case for this function 4) The code “else return binarySearch(nums, mid + 1, high, target);” is a base case for this function
Below is a recursive version of binary search:
int binarySearch(int nums[], int low, int high, int target)
{
if (low > high)
return -1;
int mid = (low + high)/2;
if (target == nums[mid])
return mid;
else if (target < nums[mid])
return binarySearch(nums, low, mid - 1, target);
else
return binarySearch(nums, mid + 1, high, target);
}
For the 4 statements below, indicate whether you think it is True or False. If you like, you
can provide a description of your answer for partial credit in case you are incorrect.
1) The code “if (low > high) return -1;” is a base case for this function
2) The code “int mid = (low + high)/2;” is a base case for this function
3) The code “if (target == nums[mid]) return mid;” is a base case for this function
4) The code “else return binarySearch(nums, mid + 1, high, target);” is a base case for
this function
Trending now
This is a popular solution!
Step by step
Solved in 2 steps