Binary Search
int binarySearch(int *arr, int n, int x)
{
//Write your code here
int start=0;
int end =n;
//int x=25;
//int arr[10]={10,12,13,15,20,25};
while(start<=end){
int mid=(start+end)/2;
if(arr[mid]==x){
return mid;
}
else if(arr[mid]>x){
end = mid-1;
}
else
start=mid+1;
}
return -1;
}
Comments
Post a Comment