Created
May 17, 2016 03:19
-
-
Save YashdalfTheGray/ea7a0e9e2ea6fd38a0294b1bd9cd0796 to your computer and use it in GitHub Desktop.
Binary Search in Javascript
This file contains 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 binarySearch(array, target) { | |
var min = 0; | |
var max = array.length-1; | |
var guess; | |
while(max >= min) { | |
guess = Math.floor((max + min)/2); | |
if(array[guess] === target) { | |
return target; | |
} else if (array[guess] < target) { | |
min = guess + 1; | |
} else { | |
max = guess - 1; | |
} | |
} | |
return -1 //solution not found. | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment