Skip to content

Instantly share code, notes, and snippets.

View paulobuchsbaum's full-sized avatar

Paulo Buchsbaum paulobuchsbaum

View GitHub Profile
@paulobuchsbaum
paulobuchsbaum / BinarySearch.js
Last active August 1, 2017 20:48
This gist implements a generic binary search. It accepts an simple array, a matrix or an array of objects. It works with ascending or descending order, in exact or approximate way.
function binarySearch(key, arr, order, exact, func) {
var low = 0;
var high = arr.length - 1;
var ind, elem;
if (arr.constructor !== Array)
return (-2) // Error
var norm = (! order || (order>=0) ); // ascending order if 0,null,...,positive
while (low <= high) {
ind = Math.floor( (low + high) / 2);