forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_sort.java
More file actions
40 lines (33 loc) · 850 Bytes
/
bucket_sort.java
File metadata and controls
40 lines (33 loc) · 850 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
import java.util.*;
//O(nlogn)
public class BucketSort{
public static void bucketSort(int[] a) {
int maxVal=a[0];
for(int i=0;i<a.length;i++){
if(a[i]>maxVal){
maxVal=a[i];
}
}
int [] bucket=new int[maxVal+1];
System.out.println("\n");
for (int i=0; i<a.length; i++) {
bucket[a[i]]++;
//System.out.print(bucket[a[i]-1]);
}
int outPos=0;
for (int i=0; i<bucket.length; i++) {
if(bucket[i]!=0){
for (int j=0; j<bucket[i]; j++) {
a[outPos++]=i;
System.out.println(bucket[i]);
}
}
}
}
public static void main(String[] args) {
int [] data= {10,23,53,22,1,1,100,32,58,34,42,64};
System.out.println("Before: " + Arrays.toString(data));
bucketSort(data);
System.out.println("After: " + Arrays.toString(data));
}
}