Package WZS. Sort; import java. util. arrays; // The bucket sort or so-called box sort. Algorithm The working principle is to divide the array into a limited number of buckets. // Each bucket is sorted individually (it is possible to use another sorting algorithm or to continue to use the bucket sorting in the progressive manner ). Bucket sorting is an inductive result of nest sorting. // When the values in the array to be sorted are evenly distributed, the linear time (NTH) is used for Bucket sorting )). However, bucket sorting is not a comparative sorting, and it is not affected by the lower limit of O (n log n. Public class binsort {public static void main (string [] ARGs) {int intarray [] = {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}; basket (intarray); system. out. println ("sorted:" + arrays. tostring (intarray);} public static void basket (INT data []) // data is the array to be sorted {int n = data. length; int bask [] [] = new int [10] [N]; int index [] = new int [10]; int max = integer. min_value; For (INT I = 0; I <n; I ++) {max = max> (integ Er. tostring (data [I]). Length ())? MAX: (integer. tostring (data [I]). length () ;}string STR; For (INT I = max-1; I> = 0; I --) {for (Int J = 0; j <N; j ++) {STR = ""; if (integer. tostring (data [J]). length () <max) {for (int K = 0; k <max-integer. tostring (data [J]). length (); k ++) STR + = "0";} STR + = integer. tostring (data [J]); bask [Str. charat (I)-'0'] [index [Str. charat (I)-'0'] ++] = data [J] ;}int Pos = 0; For (Int J = 0; j <10; j ++) {for (int K = 0; k <index [J]; k ++) {data [POS ++] = bask [J] [k] ;}} for (INT x = 0; x <10; X ++) index [x] = 0 ;}}}
Output result:
After sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]