Skip to content

Instantly share code, notes, and snippets.

@zekrom-vale
Created March 11, 2018 03:07
Show Gist options
  • Save zekrom-vale/9b2f82032ec51f97cc06404061b2e730 to your computer and use it in GitHub Desktop.
Save zekrom-vale/9b2f82032ec51f97cc06404061b2e730 to your computer and use it in GitHub Desktop.
Sort an array using binarySearch and insertionSorting to maintain efficiency.
function binarySort(l){
for(var i in l){
var m=0,M=i-1,g;
main:
while(m<=M){
if(M<=0xF0000000){
while(m<=M){
g=(M+m)>>>1;
if(l[g]===l[i])break main;
if(l[g]<l[i])m=g+1;
else M=g-1;
}
}else g=Math.floor((M+m)/2);
if(l[g]===l[i])break main;
if(l[g]<l[i])m=g+1;
else M=g-1;
}
if(l[g]<l[i])g++;
if(l[i-1]<l[i])continue;
let t=l[i];
l.splice(i, 1);
l.splice(g, 0, t);
}
return l;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment