Urgent (C++) Write a function (recursive, or not) called search which performs a
ID: 3843912 • Letter: U
Question
Urgent (C++) Write a function (recursive, or not) called search which performs a binary search on a sorted array or vector. (Recalls the binary search checks against the mid-point, and then searches the left or right subarray accordingly) Urgent (C++) Write a function (recursive, or not) called search which performs a binary search on a sorted array or vector. (Recalls the binary search checks against the mid-point, and then searches the left or right subarray accordingly) Write a function (recursive, or not) called search which performs a binary search on a sorted array or vector. (Recalls the binary search checks against the mid-point, and then searches the left or right subarray accordingly)Explanation / Answer
C++ function to perform Binary Search
void search(int array[], int size, int value)
{
int start = 0;
int end = size - 1;
int mid = (start + end)/2;
do
{
if ( array[mid] < value )
start = mid + 1;
else if ( array[mid] == value )
{
cout<<"The number "<<value<<" is found at position "<< (mid+1)<<endl;
break;
}
else
end = mid - 1;
mid = (start + end)/2;
}
while( start <= end );
if ( start > end )
cout<<"The number "<< value <<" is not found.";
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.