Skip to content

Instantly share code, notes, and snippets.

@thgreasi
Created December 26, 2013 20:14
Show Gist options
  • Save thgreasi/8138159 to your computer and use it in GitHub Desktop.
Save thgreasi/8138159 to your computer and use it in GitHub Desktop.
superFastSort with sorting function
function superFastSort(t, sortfunction, start, len) {
"use strict";
if (!sortfunction || typeof sortfunction !== 'function') {
sortfunction = function(a, b) {
return a - b;
};
}
if (start === undefined) {
start = 0;
}
if (len === undefined) {
len = t.length - start;
}
if (len > 1) {
var left = start;
var right = start + len - 1;
var base = t[right];
while (left <= right) {
while (sortfunction(t[left], base) < 0 /*t[left] < base*/) {
left++;
}
while (sortfunction(t[right], base) > 0 /*t[right] > base*/) {
right--;
}
if (left <= right) {
// swap
var tmp = t[left];
t[left] = t[right];
t[right] = tmp;
left++;
right--;
}
}
superFastSort(t, sortfunction, start, right + 1 - start);
superFastSort(t, sortfunction, right + 1, len - right - 1);
}
}
// var table = [17, 12, 6, 19, 23, 8, 5, 10];
// superFastSort(table, function(a, b){return a - b;});
// console.log(table);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment