Last active
February 14, 2018 14:57
-
-
Save c7x43t/cc977e21575e8c30c2780bd19061b304 to your computer and use it in GitHub Desktop.
Very fast sorting for Javascript - inlined swap is a bit faster
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
const quickSort = (function() { | |
var i = 0; | |
function swap(first, second, array) { | |
var temp = array[first]; | |
array[first] = array[second]; | |
array[second] = temp; | |
} | |
return function(array) { | |
for (var len = array.length - 1; i < len; i++) { | |
if (array[i] > array[i + 1]) { | |
swap(i, i + 1, array); | |
i = -1; | |
} | |
} | |
return array; | |
}; | |
})(); | |
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
const quickSort=(()=>{let a=0;return (b)=>{for(const e=b.length-1;a<e;a++)if(b[a]>b[a+1]){const d=a+1,c=b,f=c[a];c[a]=c[d];c[d]=f;a=-1}return b}})(); |
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
var quickSortI = (function() { | |
var i = 0, | |
temp = 0; | |
return function(array) { | |
for (var len = array.length - 1; i < len; i++) { | |
if (array[i] > array[i + 1]) { | |
temp = array[i]; | |
array[i] = array[i + 1]; | |
array[i + 1] = temp; | |
i = -1; | |
} | |
} | |
return array; | |
}; | |
})(); |
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
var quickSortI=(function(){var i=0,temp=0;return function(array){for(var len=array.length-1;i<len;i++){if(array[i]>array[i+1]){temp=array[i];array[i]=array[i+1];array[i+1]=temp;i=-1}} | |
return array}})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment