Last active
April 30, 2022 02:18
-
-
Save cbdavide/97100ac68e1f3699274b38a3d6bde7ba to your computer and use it in GitHub Desktop.
Binary search algorithm 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
var binarySearch = function(array, value) { | |
var guess, | |
min = 0, | |
max = array.length - 1; | |
while(min <= max){ | |
guess = Math.floor((min + max) /2); | |
if(array[guess] === value) | |
return guess; | |
else if(array[guess] < value) | |
min = guess + 1; | |
else | |
max = guess - 1; | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`//Copyright 2009 Nicholas C. Zakas. All rights reserved.
//MIT-Licensed, see source file
function binarySearch(items, value){
}`