Bucket sort is holding space for time, in particular the number of large, barrel a lot of time faster than some sort of fast. (However, if the number of a million may run out of memory (array too long))
Set 10 array (on behalf of No. 0-9 barrels)
Press into a bit corresponding to the bucket, and then successively taken back into the array.
Then press ten. . .
hundreds. . .
。。。
Until highest bits are 0.
123,125,214,243,222
The first (bit):
2: 222
3:123,243
4:214
5:125
Then remove: 222,123,243,214,125
Second (10)
1:214
2:222,123,125
4:243
Then remove: 214,222,123,125,243
Third (100)
1:123,125
2:214,222,243
Then remove: 123,125,214,222,243
Not 1000, so the end of the sort
In fact, the essence is to compare bit.
// bucket sort public void bucketSort ( int [] A) { // first to get the maximum number of bits, determines the number of loops int max = A [0 ]; for ( int I = 0; I <a.length; I ++ ) { IF (max < A [I]) { max = A [I]; } } // at this time is the maximum value max int MAXLENGTH = ( "" + max) .length (); // put into max then take the length of the string, to obtain the number of bits int n-= 1; // dividend is the first one, the second is 10 (= 1212/10 this example, 121, then on the modulo 10, to obtain a 10-bit) a third time .. 100 for ( int0 = K; K <MAXLENGTH; K ++, n-n-* = 10) { // each dividend 10 * int [] [] = bucket new new int [10] [a.length]; // 10 barrels int [] = b_max new new int [10]; // int default value number of data records of the barrel 10 to facilitate insertion of java is 0, it can not be assigned. for ( int J = 0; J <a.length; J ++ ) { int NUM = (A [J] /% n-10); // get the current position with a modulo ps:% and / or the same priority, so the error is calculated left to right bucket [num] [b_max [num ]] = A [J]; b_max [NUM] ++ ; } // after the tub is done, remove the back loaded int= 0 a_max; // with the back and from a memory for ( int I = 0; I <10; I ++) { // iterate the tub 10 for ( int J = 0; J <b_max [I]; J ++) { // wherein the i-th bucket traversing a [a_max] = bucket [i] [J]; a_max ++ ; } } } // after the cycle is completed, a value of the already sorted. }