40 lines
860 B
JavaScript
40 lines
860 B
JavaScript
function Sort() {
|
|
return {
|
|
defaultCompare: defaultCompare,
|
|
map: map,
|
|
insert: insert,
|
|
};
|
|
|
|
function insert(obj, t, compare, min, max) {
|
|
min = min == undefined ? 0 : min;
|
|
max = max == undefined ? t.length : max;
|
|
compare = compare == undefined ? defaultCompare : compare;
|
|
if(max - min < 1) {
|
|
return min;
|
|
}
|
|
var avg = Math.floor((max + min) / 2);
|
|
if (compare(obj, t[avg]) < 0) {
|
|
return insert(obj, t, compare, min, avg);
|
|
} else {
|
|
return insert(obj, t, compare, avg+1, max);
|
|
}
|
|
}
|
|
|
|
function defaultCompare(a, b) {
|
|
if(a < b) {
|
|
return -1;
|
|
} else if(a > b) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function map(projector, f) {
|
|
return function() {
|
|
var args = Array.prototype.map.call(arguments, projector);
|
|
return f.apply(null, args);
|
|
}
|
|
}
|
|
|
|
}
|