BucketSorter implements Sorter<Double> { @Override public void sort(Double[] items) { double min = Integer.MAX_VALUE; double max = Integer.MIN_VALUE; for (Double item : items) { if (min > item) { min = item; } if (max < item) { max = item; } } double bucketSize = (max - min) / (this.buckets - 1.0); final List<List<Double>> buckets = new ArrayList<>(this.buckets); for (int i = 0; i < this.buckets; i ++) { buckets.add(new LinkedList<Double>()); } for (Double item : items) { buckets.get((int) ((item - min) / bucketSize)).add(item); } int copied = 0; for (final List<Double> bucket : buckets) { final Double[] values = bucket.toArray(new Double[bucket.size()]); sorter.sort(values); System.arraycopy(values, 0, items, copied, values.length); copied += values.length; } } BucketSorter(int buckets); @Override void sort(Double[] items);  }
@Test public void testSortingEmptyArray() throws Exception { final Double[] items = {}; getAscendingSorter().sort(items); assertThat(items, emptyArray()); }
@Test public void testSimpleAscendingSortWithNegativeItems() throws Exception { final Double[] items = {-1.7, -1.9, -1.8, 1.7, 1.9, 1.8, 0.0}; final int length = items.length; getAscendingSorter().sort(items); assertThat(items, arrayWithSize(length)); assertThat(items, arrayContaining(-1.9, -1.8, -1.7, 0.0, 1.7, 1.8, 1.9)); }
@Test public void testAscendingSortWithInfinity() throws Exception { final Double[] items = {1., 2., 3., (double) Integer.MIN_VALUE, 0., (double) Integer.MAX_VALUE, 4., 5., 6.}; final int length = items.length; getAscendingSorter().sort(items); assertThat(items, arrayWithSize(length)); assertThat(items, arrayContaining((double) Integer.MIN_VALUE, 0., 1., 2., 3., 4., 5., 6., (double) Integer.MAX_VALUE)); }
@Test(dataProvider = "normalDataProvider") public void testSortIntegrity(Double[] items, Double[] expected) throws Exception { final Double[] target = new Double[items.length]; System.arraycopy(items, 0, target, 0, target.length); getAscendingSorter().sort(target); assertThat(target, arrayWithSize(expected.length)); assertThat(target, arrayContaining(expected)); }