Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created October 12, 2010 20:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdoob/622846 to your computer and use it in GitHub Desktop.
Save mrdoob/622846 to your computer and use it in GitHub Desktop.
// Ported from http://blog.inspirit.ru/wp-content/uploads/qsort/Main.as
function flashSort( array ) {
var a = array, n = array.length;
var i = 0, j = 0, k = 0, t, hold, flash;
var m = ~~( n * 0.125 );
var l = [], anmin = a[ 0 ], nmax = 0, nmove = 0;
while ( ++i < n ) {
if ( a[ i ] < anmin ) anmin = a[ i ];
if ( a[ i ] > a[ nmax ] ) nmax = i;
}
if ( anmin == a[ nmax ] ) return;
var c1 = ( m - 1 ) / ( a[ nmax ] - anmin );
i = 0;
while ( i < m ) l[ i++ ] = 0;
i = 0;
while ( i < n ) {
k = ~~( c1 * ( a[ i++ ] - anmin ) );
++l[ k ];
}
k = 0;
while ( ++k < m ) {
l[ k ] += l[ k - 1 ];
}
hold = a[ nmax ];
a[ nmax ] = a[ 0 ];
a[ 0 ] = hold;
j = 0;
k = m - 1;
i = n - 1;
while ( nmove < i ) {
while ( j > ( l[ k ] - 1 ) ) {
k = ~~( c1 * ( a[ ++j ] - anmin ) );
}
flash = a[ j ];
while ( j != l[ k ] ) {
k = ~~( c1 * ( flash - anmin ) );
hold = a[ ( t = l[ k ]-1) ];
a[ t ] = flash;
flash = hold;
--l[ k ];
++nmove;
}
}
j = 0;
while ( ++j < n ) {
hold = a[ j ];
i = j - 1;
while ( i >= 0 && a[ i ] > hold ) {
a[ i + 1 ] = a[ i-- ];
}
a[ i + 1 ] = hold;
}
}
@FrancisVarga
Copy link

Sorting algorithmen?or what that? :D

@mrdoob
Copy link
Author

mrdoob commented Oct 12, 2010

Yup :)
Seems to be 2x times faster than array.sort( function ( a, b ) { return a - b; } );

@FrancisVarga
Copy link

nice to know...
but it's very compressed and it's unreadable...

@mrdoob
Copy link
Author

mrdoob commented Oct 12, 2010

Yeah, it's designed to be fast rather than readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment