Created
December 26, 2013 20:14
-
-
Save thgreasi/8138159 to your computer and use it in GitHub Desktop.
superFastSort with sorting function
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 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