In: Computer Science
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration.
42 39 2 6 9 16 20 28 31 34
SOLUTION :
CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION.
C++ CODE :
#include <iostream>
using namespace std;
int unimodalMin(int arr[],int low,int high)
{
if(low==high-1)
return arr[low];
int mid = (high + low)/2;
printf("right inedex=%d,left inedex=%d,mid
index=%d\n",low,high,mid);
if(arr[mid]>arr[mid+1])
return unimodalMin(arr,mid+1,high);
return unimodalMin(arr,low,mid+1);
}
int main()
{
int arr[] = {42,39,2,6,9,16,20,28,31,34};
int ans = unimodalMin(arr,0,9);
printf("\nmin element is = %d",ans);
return 0;
}
NOTE : PLEASE UPVOTE ITS VERY MUCH IMPORTANT FOR ME A LOT. PLZZZ......