Created
March 11, 2018 03:07
-
-
Save zekrom-vale/9b2f82032ec51f97cc06404061b2e730 to your computer and use it in GitHub Desktop.
Sort an array using binarySearch and insertionSorting to maintain efficiency.
This file contains hidden or 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 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