Created
October 12, 2010 20:27
-
-
Save mrdoob/622846 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, it's designed to be fast rather than readable.