-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketSort.java
More file actions
50 lines (38 loc) · 870 Bytes
/
BucketSort.java
File metadata and controls
50 lines (38 loc) · 870 Bytes
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
package geeks.algo.sort;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BucketSort extends Sort {
BucketSort(int[] arr) {
super(arr);
}
@Override
protected void performSort() {
int n = arr.length;
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i=0;i<n;i++){
List<Integer> ls = list.get(n * arr[i]);
if(null == ls){
ls=new ArrayList<Integer>();
}
ls.add(arr[i]);
list.set(n*arr[i], ls);
}
for(List<Integer> ls:list){
Integer[] tmp = ls.toArray(new Integer[ls.size()]);
Arrays.sort(tmp);
}
int i=0;
for(List<Integer> ls:list){
for(int j=0;j<ls.size();j++){
arr[i++] = ls.get(j);
}
}
}
@Override
protected void setComplexities() {
this.timeComplexity="O(n * k)";
this.spaceComplexity="O(n)";
this.type="Bucket Sort";
}
}