Last active
December 17, 2015 13:29
-
-
Save dotfold/5618007 to your computer and use it in GitHub Desktop.
binary searchy matchy thingy
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
(function() { | |
// Given an array of values, find all exact matches. | |
// If no matches are found, find the closest match. | |
// | |
// Returns an array of indices where matches occur | |
// If no exact match, a closest match is performed and returned as an array with one element | |
// | |
function search(values, desired) { | |
var low = 0, high = values.length, result = []; | |
while (low < high) { | |
var mid = (low + high) >>> 1; | |
values[mid] < desired | |
? low = mid + 1 | |
: high = mid; | |
} | |
if (values[low] == desired) { | |
result.push(low); | |
// because it's an ordered list, just check onwards | |
while(values[++low] === desired) { | |
result.push(low); | |
} | |
} | |
if (result.length > 0) { | |
return result; | |
} | |
if (values[low] !== desired) { | |
var diff = [values[low - 1], values[low]].reduce(function(memo, val) { | |
var d1 = Math.abs(memo - desired); | |
var d2 = Math.abs(val - desired); | |
return d1 > d2 ? val : memo; | |
}); | |
low = values.indexOf(diff); | |
} | |
return [low]; | |
} | |
// tests | |
var f = search([60,72,100,120,180], 100); | |
var g = search([60,72,100,100,100,120,180], 100); | |
var h = search([60,72,100,100,100,120,180], 60); | |
var i = search([60,72,100,100,100,120,180], 180); | |
var j = search([60,72,100,100,100,120,180], 170); | |
var k = search([60,72,100,100,100,120,180], 65); | |
var l = search([90,100,400,400,600,700], 200); | |
var m = search([90,100,201,400,600,700], 200); | |
console.log('f: ' + (f.length == 1 && f[0] == 2)); | |
console.log('g: ' + (g.length == 3 && g[0] == 2 && g[1] == 3 && g[2] == 4)); | |
console.log('h: ' + (h.length == 1 && h[0] == 0)); | |
console.log('i: ' + (i.length == 1 && i[0] == 6)); | |
console.log('j: ' + (j.length == 1 && j[0] == 6)); | |
console.log('k: ' + (k.length == 1 && k[0] == 0)); | |
console.log('l: ' + (l.length == 1 && l[0] == 1)); | |
console.log('m: ' + (m.length == 1 && m[0] == 2)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment