Skip to content

Instantly share code, notes, and snippets.

@c7x43t
Created July 31, 2017 21:19
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 c7x43t/63c03f57b22afab6e9789ca146485138 to your computer and use it in GitHub Desktop.
Save c7x43t/63c03f57b22afab6e9789ca146485138 to your computer and use it in GitHub Desktop.
// 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