Input: 1,3,1,2,3,1,2,2,3
Output: 1, 1, 1, 2, 2, 2, 3, 3, 3
import java.util.ArrayList;
public class BucketSort {
ArrayList
private int count;
public BucketSort(int unique_int_count) {
this.count = unique_int_count;
this.bucket = new ArrayList
for(int i=0; i
}
public ArrayList
for(int c:input) {
if(c==1) {
bucket.get(0).add(c);
} else if(c==2) {
bucket.get(1).add(c);
} else {
bucket.get(2).add(c);
}
}
ArrayList
for(int i=0; i
return sorted_output;
}
@Override
protected void finalize() throws Throwable {
bucket=null;
super.finalize();
}
}
//----------------------Main Method------------------------
public static void main(String[] args) {
BucketSort b = new BucketSort(3);
ArrayList
input.add(1);
input.add(3);
input.add(1);
input.add(2);
input.add(3);
input.add(1);
input.add(2);
input.add(2);
input.add(3);
System.out.println(b.Sort(input));
}
No comments:
Post a Comment