Created
April 3, 2019 04:40
-
-
Save IAMIronmanSam/26c4ee6ebc16e118057cf7535d39a354 to your computer and use it in GitHub Desktop.
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
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