Array Question 19
Question 19. Binary Search
Program
class Solution { int binarysearch(int arr[], int n, int k) { int low=0; int high=n-1; int res=-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==k) { res=mid; break; } else if(arr[mid]<k) { low=mid+1; } else if(arr[mid]>k){ high=mid-1; } } return res; } } |