Created
August 12, 2015 06:17
-
-
Save nicoptere/af0e73e680b318ef1d81 to your computer and use it in GitHub Desktop.
sorts an array by storing highest values in the "center"
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
//sorts an array by storing highest values in the "center" | |
function sortGauss( array ) | |
{ | |
array.sort( function(a,b) | |
{ | |
return a - b; | |
}); | |
var a = []; | |
var b = []; | |
while( array.length > 0 ) | |
{ | |
a.push( array.shift() ); | |
if( array.length <= 0 )break; | |
b.push( array.shift() ); | |
} | |
b.reverse(); | |
return a.concat( b ); | |
} | |
var a = [0, 2, 7, 0, 0, 3, 2, 4, 1, 2, 1, 0, 0, 0, 1, 8, 1, 0]; | |
console.log( "->", sortGauss(a)); //-> [0, 0, 0, 0, 1, 1, 2, 3, 7, 8, 4, 2, 2, 1, 1, 0, 0, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment