PackageCom.edu.hpu.sort.bucket;Importjava.util.LinkedList;Importjava.util.List;ImportCom.edu.hpu.sort.Sort;/*sorting principle: The sequence from the array to remove the number, first 6 is taken out, and then put 6 into the 6th bucket, the process is similar to this: empty bucket [array [0]] = array to be sorted [0] [6 2 4 1 5 9] Array to be sorted [0 0 0 0 0 0 6 0 0 0] Empty barrels [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present) order from the array to be sorted out the next number, at which time 2 is taken out, put it into the bucket 2nd, is a few barrels [6 2 4 1 5 9] arrays to be queued [0 0 2 0 0 0 6 0 0 0] Empty barrels [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present) 3,4,5,6 omitted, the process is the same, all into the bucket and then into the bottom of this [6 2 4 1 5 9] Array [0 1 2 0 4 5 6 0 0 9] Empty bucket [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)*/ Public classBucketsortextendsSort {Private intRange = 0; PublicBucketsort (intrange) { This. Range =range; } @Override Public int[] Dosort (int[] arr) {@SuppressWarnings ("Unchecked") //Constructing auxiliary Arrayslist<integer> [] aux =NewLinkedlist[range]; for(inti = 0; i < aux.length; i++) {Aux[i]=NewLinkedlist<integer>(); } for(inti = 0; i < arr.length; i++){ //find the position of the element in the bucket and add itAux[arr[i]].add (Arr[i]); } for(inti = 0, j = 0; I < Aux.length && J < Arr.length; i++){ for(intV:aux[i]) {Arr[j]=v; J++; } } returnarr; } Public Static voidMain (string[] args) {sort sort=NewBucketsort (10); Sort.printorder (New int[]{4, 1, 3, 2, 6, 9, 9}); } }
Java implementation of bucket sequencing