Last active
September 24, 2022 14:41
-
-
Save Vaib215/14bfd01c6e0dab5cb9b6f2fc748be2ee to your computer and use it in GitHub Desktop.
Time Complexity = O(log n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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