Skip to content

Instantly share code, notes, and snippets.

@lifeparticle
Created February 26, 2021 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeparticle/d80e83621a01f705ab73ddd1b9c627cb to your computer and use it in GitHub Desktop.
Save lifeparticle/d80e83621a01f705ab73ddd1b9c627cb to your computer and use it in GitHub Desktop.
public static int binarySearch (int values[], int target) {
int left = 0;
int mid = 0;
int right = values.length - 1;
while (left <= right) {
mid = (left + right) / 2;
if (target == values[mid])
return mid;
else if(target > values[mid])
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment