Last active
December 27, 2015 08:09
-
-
Save thgreasi/7293898 to your computer and use it in GitHub Desktop.
superFastSort.js
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 , start, len ) { | |
"use strict"; | |
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 (t[left] < base) { | |
left++; | |
} | |
while (t[right] > base) { | |
right--; | |
} | |
if (left <= right) { | |
// swap | |
var tmp = t[left]; | |
t[left] = t[right]; | |
t[right] = tmp; | |
left++; | |
right--; | |
} | |
} | |
superFastSort(t, start, right + 1 - start); | |
superFastSort(t, right + 1, len - right - 1); | |
} | |
} | |
// var table = [17, 12, 6, 19, 23, 8, 5, 10]; | |
// superFastSort(table); | |
// console.log(table); |
Is there a version that works with negative numbers?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jsperf tests