Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Created July 19, 2014 19:13
Show Gist options
  • Select an option

  • Save metallurgix/274d35122c09fb0c60bf to your computer and use it in GitHub Desktop.

Select an option

Save metallurgix/274d35122c09fb0c60bf to your computer and use it in GitHub Desktop.
Binary Search
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