Skip to content

Commit 648ef77

Browse files
sjmillingtonpivovarit
authored andcommitted
[BAEL-3193] Bucket Sort In Java Code (eugenp#7731)
1 parent 1fc511b commit 648ef77

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.bucketsort;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class IntegerBucketSorter implements Sorter<Integer> {
8+
9+
private final Comparator<Integer> comparator;
10+
11+
public IntegerBucketSorter(Comparator<Integer> comparator) {
12+
this.comparator = comparator;
13+
}
14+
15+
public IntegerBucketSorter() {
16+
comparator = Comparator.naturalOrder();
17+
}
18+
19+
public List<Integer> sort(List<Integer> arrayToSort) {
20+
21+
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
22+
23+
for(List<Integer> bucket : buckets){
24+
bucket.sort(comparator);
25+
}
26+
27+
return concatenateSortedBuckets(buckets);
28+
}
29+
30+
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
31+
List<Integer> sortedArray = new ArrayList<>();
32+
int index = 0;
33+
for(List<Integer> bucket : buckets){
34+
for(int number : bucket){
35+
sortedArray.add(index++, number);
36+
}
37+
}
38+
return sortedArray;
39+
}
40+
41+
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
42+
43+
final int[] codes = createHashes(initialList);
44+
45+
List<List<Integer>> buckets = new ArrayList<>(codes[1]);
46+
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
47+
48+
//distribute the data
49+
for (int i : initialList) {
50+
buckets.get(hash(i, codes)).add(i);
51+
}
52+
return buckets;
53+
54+
}
55+
56+
private int[] createHashes(List<Integer> input){
57+
int m = input.get(0);
58+
for (int i = 1; i < input.size(); i++) {
59+
if (m < input.get(i)) {
60+
m = input.get(i);
61+
}
62+
}
63+
return new int[]{m, (int) Math.sqrt(input.size())};
64+
}
65+
66+
private static int hash(int i, int[] code) {
67+
return (int) ((double) i / code[0] * (code[1] - 1));
68+
}
69+
70+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.bucketsort;
2+
3+
import java.util.List;
4+
5+
public interface Sorter<T> {
6+
7+
List<T> sort(List<T> arrayToSort);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.bucketsort;
2+
3+
import com.baeldung.bucketsort.IntegerBucketSorter;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class IntegerBucketSorterUnitTest {
13+
14+
private IntegerBucketSorter sorter;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
sorter = new IntegerBucketSorter();
19+
}
20+
21+
@Test
22+
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
23+
24+
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
25+
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
26+
27+
List<Integer> actual = sorter.sort(unsorted);
28+
29+
assertEquals(expected, actual);
30+
31+
32+
}
33+
}

0 commit comments

Comments
 (0)