Skip to content

Instantly share code, notes, and snippets.

@nicoptere
Created August 12, 2015 06:17
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 nicoptere/af0e73e680b318ef1d81 to your computer and use it in GitHub Desktop.
Save nicoptere/af0e73e680b318ef1d81 to your computer and use it in GitHub Desktop.
sorts an array by storing highest values in the "center"
//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