/*
* Binary search in JavaScript.
* Returns the index of of the element in a sorted array or (-n-1) where n is the insertion point for the new element.
* Parameters:
* ar - A sorted array
* el - An element to search for
* compare_fn - A comparator function. The function takes two arguments: (a, b) and returns:
* a negative number if a is less than b;
* 0 if a is equal to b;
* a positive number of a is greater than b.
* The array may contain duplicate elements. If there are more than one equal elements in the array,
* the returned value can be the index of any one of the equal elements.
*/
function binarySearch(ar, el, compare_fn) {
var m = 0;
var n = ar.length - 1;
while (m <= n) {
var k = (n + m) >> 1;
var cmp = compare_fn(el, ar[k]);
if (cmp > 0) {
m = k + 1;
} else if(cmp < 0) {
n = k - 1;
} else {
return k;
}
}
return -m - 1;
}
function compare_number(a, b) {
return a - b;
}
new function test() {
var ar = [1, 2, 2, 2, 5, 9, 11, 12, 12, 12, 12, 15, 20, 20, 20, 25, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 80];
for(var i = 0; i <= 100; i++) {
var n = binarySearch(ar, i, compare_number);
document.body.appendChild(document.createTextNode("binarySearch("+i+") = "+n));
if((n >= 0 && ar[n] !== i) ||
(n < 0 && ((-n-1) < ar.length && ar[-n-1] <= i)) ||
(n < 0 && (-n-2 >= 0 && ar[-n-2] >= i))) {
document.body.appendChild(document.createTextNode(" //fail"));
}
else {
document.body.appendChild(document.createTextNode(" //correct"));
}
document.body.appendChild(document.createElement("br"));
}
}();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.