I wrote a binary search in javascript.
Array.prototype.binarySearch = function(find) {
var low = 0, high = this.length - 1,
i;
while (low <= high) {
i = Math.floor((low + high) / 2);
if (this[i] > find) { low = i; continue; };
if (this[i] < find) { high = i; continue; };
return i;
}
return null;
}
It fails though to find 5 in my array of integers.
var intArray = [1, 2, 3, 5]
if (intArray.binarySearch(5))
alert("found!");
else
alert("no found!");
Here is a Fiddle.
http://jsfiddle.net/3uPUF/3/
You have the logic backward for changing low and high,
if this[i] > findthen you want to look between 1 and i-1.If this[i] < findthen you want to look between i+1 and the length of the array.Try making these changes: