Question: In C++, make the following binary search function work on an array of strings to give you the index location of the string you want
In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only.
int binarySearch(int arr[], int firstIndex, int lastIndex, int target){
int index;
if (firstIndex>lastIndex)
index = -1;
else {
int mid = firstIndex + (lastIndex - firstIndex) / 2;
if (target == arr[mid])
index = mid;
else if (target < arr[mid])
index = binarySearch(arr, firstIndex, mid - 1, target);
else
index = binarySearch(arr, mid + 1, lastIndex, target);
}
return index;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
