Skip to content

Instantly share code, notes, and snippets.

@IAMIronmanSam
Created April 3, 2019 04:40
Show Gist options
  • Save IAMIronmanSam/26c4ee6ebc16e118057cf7535d39a354 to your computer and use it in GitHub Desktop.
Save IAMIronmanSam/26c4ee6ebc16e118057cf7535d39a354 to your computer and use it in GitHub Desktop.
function search(array, val) {
let min = 0;
let max = array.length - 1;
while (min <= max) {
let middle = Math.floor((min + max) / 2);
let currentElement = array[middle];
if (array[middle] < val) {
min = middle + 1;
}
else if (array[middle] > val) {
max = middle - 1;
}
else {
return middle;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment