#include <stdio.h>
int binarysearch(int arr[],int len,int target);
int main()
{
int arr[100],n,res,i,target;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
scanf("%d",&target);
res=binarysearch(arr,n-1,target);
printf("%d",res);
}
int binarysearch(int arr[],int len,int target)
{
int hi=len,lo=0;
while(lo<=hi)
{
int mid=(lo+hi)/2;
if(arr[mid]==target)
{
return mid;
}
else if(arr[mid]<target)
{
lo=mid+1;
}
else
{
hi=mid-1;
}
}
return -1;
}
7
5 9 11 12 14 16 17
11
output:
2
No comments:
Post a Comment