JSFiddle - React, Tailwind, and code Playground
by gkucmierz
JavaScript
var arr = [];//, 2, 4, 5, 9, 12, 22, 22, 31, 45, 67];//.reverse();
console.log(arr.join(' '));
var insert = function(el, arr) {
if (arr.length === 0) return arr.push(el);
return arr.splice(locationOf(el, arr, 0, arr.length, 100), 0, el);
}
var cmpFn = function(a, b, pivot) {
console.log(a, b, pivot);
return (a - b) * 1;
};
var locationOf = function callee(element, array, start, end, max, lastDiff) {
var diff = end - start;
var pivot = start + (diff * 0.5 >> 0);
var cmpRes = cmpFn(array[pivot], element, pivot);
console.log(start+' - '+end+' => '+pivot+' ('+diff+')');
if (max===0) return console.log('break');
if (diff === 1 && lastDiff === 1) {
console.log('++');
return pivot + 1;
}
if (end-start === 0 || cmpRes === 0) return pivot;
if (cmpRes < 0) {
return callee(element, array, pivot, end, max-1, diff);
} else {
return callee(element, array, start, pivot, max-1, diff);
}
};
var el = 3;
console.log(locationOf(el, arr, 0, arr.length, 10));
insert(el, arr);
console.log(arr.join(' '));