Skip to content

Instantly share code, notes, and snippets.

@mrdoob
Created October 12, 2010 20:27
Show Gist options
  • Select an option

  • Save mrdoob/622846 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Sorting algorithmen?or what that? :D

@mrdoob

mrdoob commented Oct 12, 2010

Copy link
Copy Markdown
Author

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

@FrancisVarga

Copy link
Copy Markdown

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

@mrdoob

mrdoob commented Oct 12, 2010

Copy link
Copy Markdown
Author

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