Created
July 19, 2014 19:13
-
-
Save metallurgix/274d35122c09fb0c60bf to your computer and use it in GitHub Desktop.
Binary Search
This file contains hidden or 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[] nums, int key) | |
| { | |
| int hi = nums.length - 1; | |
| int lo = 0; | |
| int mid | |
| while(hi >= lo) | |
| { | |
| mid = lo + ((hi - lo) / 2); | |
| if(nums[mid] > key) | |
| hi = mid - 1; | |
| else if(nums[mid] < key) | |
| lo = mid + 1; | |
| else | |
| return mid; | |
| } | |
| return -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment