import java.util.*; /** * Bucket Sort * * * Input: * arr: A list of small ints * k: Upper bound of the size of the ints in arr (not inclusive) * * Precondition: * all(isinstance(x, int) and 0 <= x < k for x in arr) * * Output: * The elements of arr in sorted order * * @author derricklin */ public class BUCKETSORT { public static ArrayList bucketsort(ArrayList arr, int k) { ArrayList counts = new ArrayList(Collections.nCopies(k,0)); for (Integer x : arr) { counts.set(x,counts.get(x)+1); } ArrayList sorted_arr = new ArrayList(100); int i = 0; for (Integer count : counts) { sorted_arr.addAll(Collections.nCopies(count, i)); i++; } return sorted_arr; } }