-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBucketSort.java
More file actions
42 lines (33 loc) · 1.02 KB
/
Copy pathBucketSort.java
File metadata and controls
42 lines (33 loc) · 1.02 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
package Sort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class BucketSort {
public static void bucketSort(int[] nums, int bucketNum){
List<ArrayList<Integer>> buckets = new ArrayList<>();
for (int i=0; i< bucketNum;i++){
buckets.add(new ArrayList<>());
}
for(int i=0; i<nums.length; i++){
buckets.get(nums[i]/10).add(nums[i]);
}
for(int i=0; i<bucketNum;i++){
Collections.sort(buckets.get(i));
}
int k=0;
for(int i =0; i<bucketNum;i++){
for(int j=0; j<buckets.get(i).size();j++){
if(j==buckets.get(i).size()){
break;
}
nums[k++] = buckets.get(i).get(j);
}
}
}
public static void main(String[] args){
int[] nums = {2,9,7,17,12,46,42,74,75,88,91};
bucketSort(nums, 10);
System.out.println(Arrays.toString(nums));
}
}