Skip to content

Instantly share code, notes, and snippets.

@hdf
Last active December 22, 2018 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdf/309f3803461424f5147521c970615edf to your computer and use it in GitHub Desktop.
Save hdf/309f3803461424f5147521c970615edf to your computer and use it in GitHub Desktop.
function bubbleSort(arr, cmp) {
var len = arr.length, tmp, n;
if (typeof cmp !== 'function')
cmp = function(a, b) {
return a > b;
};
do {
n = 0;
for (var i = 1; i < len; i++) {
if (cmp(arr[i], arr[i-1])) continue;
n = i;
tmp = arr[i-1];
arr[i-1] = arr[i];
arr[i] = tmp;
}
len = n;
} while (len > 0);
return arr;
}
function hdfSort(arr, comparer) {
var len = arr.length;
if (len < 2) return arr;
var tmp = arr;
if (typeof comparer !== 'function')
comparer = function(a, b) {
return a > b;
};
var l2 = Math.ceil(len / 2);
var ret = new Array(len);
var smallest, greatest;
for (var i = 0; i < l2; i++) {
for (var i2 = 0; i2 < len; i2++) {
if (tmp[i2] == null) continue;
if (smallest == null || comparer(tmp[smallest], tmp[i2])) smallest = i2;
if (greatest == null || comparer(tmp[i2], tmp[greatest])) greatest = i2;
}
ret[i] = tmp[smallest];
ret[len-i-1] = tmp[greatest];
tmp[smallest] = null;
tmp[greatest] = null;
greatest = smallest = null;
}
return ret;
}
function insertSort(arr, cmp) {
var len = arr.length, tmp, j;
if (typeof cmp !== 'function')
cmp = function(a, b) {
return a > b;
};
for (var i = 1; i < len; i++) {
tmp = arr[i];
j = i - 1;
while (j >= 0 && cmp(arr[j], tmp)) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = tmp;
}
return arr;
}
var mergeSort = (function () {
var cmp = {};
var default_cmp = function(a, b) {
return a < b;
};
function merger(arr) {
if (arr.length < 2) return arr;
let pivot = Math.ceil(arr.length / 2);
return merge(merger(arr.slice(0, pivot)), merger(arr.slice(pivot)));
}
function merge(left, right) {
var res = [], l = 0, r = 0;
while(l < left.length && r < right.length)
res.push(cmp(left[l], right[r]) ? left[l++] : right[r++]);
return res.concat(left.slice(l), right.slice(r));
}
return function (arr, comparer) {
cmp = (typeof comparer === 'function') ? comparer : default_cmp;
return merger(arr);
};
}());
Array.prototype.quickSort = function(cmp) {
var len = this.length;
if (len < 2) return this;
var arr = this;
if (typeof cmp !== 'function')
cmp = function(a, b) {
return a < b;
};
var less = [], greater = [];
var pivot = arr.splice(Math.floor(len-- / 2), 1);
for (var i = 0; i < len; i++) {
if (cmp(arr[i], pivot))
less.push(arr[i]);
else
greater.push(arr[i]);
}
return less.quickSort(cmp).concat(pivot, greater.quickSort(cmp));
};
@hdf
Copy link
Author

hdf commented Jul 22, 2018

Benchmark:
http://jsben.ch/qVHSf
Interestingly, depending on how large the array is, the results can be totally different. The takeaway however is, that the larger the array, the more recursion costs, and at some point it even offsets the cost of extra iterations. (Probably due to a lack of Tail Call Optimization.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment