-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketSort.java
More file actions
69 lines (53 loc) · 2.01 KB
/
BucketSort.java
File metadata and controls
69 lines (53 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package kb.sort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import kb.sort.api.Sortable;
public class BucketSort implements Sortable {
@Override
public int[] sort(int[] nums) {
// find the max number
int max = 0;
for (int num : nums)
if (num > -1)
max = Math.max(num, max);
else
throw new UnsupportedOperationException("Negative numbers are not supported.");
// initialize the buckets
int n = nums.length;
@SuppressWarnings("unchecked")
List<Integer>[] buckets = new ArrayList[n];
for (int i = 0; i < buckets.length; i++)
buckets[i] = new ArrayList<>();
// add the input numbers the the bucket with hashing function. The hashing
// function should guarantees that:
// if `num_i <= num_j` then `hash(num_i) <= hash(num_j)` where 0 <= and hash(num) < n
for (int num : nums)
buckets[(int) ((num * 0.1) / max * n)].add(num);
// Sort each bucket
for (List<Integer> bucket : buckets)
insertionSort(bucket); // or Collections.sort(bucket)
// put back the numbers to the input/new array
int i = 0;
for (List<Integer> bucket : buckets)
for (int num : bucket)
nums[i++] = num;
return nums;
}
public void insertionSort(List<Integer> nums) {
for (int i = 1; i < nums.size(); i++) {
int curr = nums.get(i);
int j = i - 1;
while (j >= 0 && nums.get(j) > curr)
nums.set(j + 1, nums.get(j--));
nums.set(j + 1, curr);
}
}
public static void main(String[] args) {
Sortable bSort = new BucketSort();
int[] arr = { 1, 4, 1, 2, 7, 5, 2 };
System.out.println(Arrays.toString(bSort.sort(arr)));
int[] arr1 = { 9, 10, 1, 0, 20, 6, 5, 4, 1, 3, 3, 2, 0, 7, 9, 8 };
System.out.println(Arrays.toString(bSort.sort(arr1)));
}
}