-
-
Save dfparker2002/7aa558ea12fa0a3da8907f537a450c12 to your computer and use it in GitHub Desktop.
Binary Search
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 binary_search = function (list, item) { | |
var low = 0; | |
var high = list.length - 1; | |
while(low <= high) { | |
var mid = Math.floor((low + high) / 2); | |
var guess = list[mid]; | |
if (guess === item) { | |
return mid; | |
} else if (guess > item) { | |
high = mid - 1; | |
} else { | |
low = mid + 1; | |
} | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment