Skip to content

Instantly share code, notes, and snippets.

@Vaib215
Last active September 24, 2022 14:41
Show Gist options
  • Save Vaib215/14bfd01c6e0dab5cb9b6f2fc748be2ee to your computer and use it in GitHub Desktop.
Save Vaib215/14bfd01c6e0dab5cb9b6f2fc748be2ee to your computer and use it in GitHub Desktop.
Time Complexity = O(log n)
public static int binarySearch(int arr[], int key){
int start = 0, end = arr.length - 1;
while(start<=end){
int mid = start + (end-start)/2;
if(arr[mid]==key) return mid;
else if(arr[mid]>key) end = mid - 1;
else start = mid + 1;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment