Created
July 31, 2017 21:19
-
-
Save c7x43t/63c03f57b22afab6e9789ca146485138 to your computer and use it in GitHub Desktop.
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
// fast array sort | |
function heap3Sort() { | |
function swap(ary, a, b) { | |
var t = ary[a]; | |
ary[a] = ary[b]; | |
ary[b] = t; | |
} | |
function shiftDown(ary, start, end) { | |
var root = start, | |
child, s, root21; | |
while ((root21 = (root << 1) + 1) <= end) { | |
child = root21; | |
s = root; | |
if (ary[s] < ary[child]) { | |
s = child; | |
} | |
var child1 = child + 1; | |
if (child1 <= end && ary[s] < ary[child1]) { | |
s = child1; | |
} | |
if (s !== root) { | |
swap(ary, root, s); | |
root = s; | |
} else { | |
return; | |
} | |
} | |
} | |
return (function(){return function(ary) { | |
ary=ary.slice(0); | |
const len = ary.length; | |
for (var start = (len >>> 1) - 1; start >= 0; start--) { | |
shiftDown(ary, start, len - 1); | |
} | |
for (var end = len - 1; end > 0; end--) { | |
swap(ary, end, 0); | |
shiftDown(ary, 0, end - 1); | |
} | |
return ary; | |
};}()) | |
} | |
const arrayHeapSort=heap3Sort(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment