Created
August 9, 2016 02:16
-
-
Save mrsweaters/ca9f22cc3d190f5c94e41aec76bb8a95 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