Bucket sort java O(n)


#1

public class Solution {
public void sortColors(ArrayList a) {
ArrayList r = new ArrayList<>();
ArrayList w = new ArrayList<>();
ArrayList b = new ArrayList<>();
for(int i=0;i<a.size();i++){
int I=a.get(i);
if(I==0)r.add(I);
else if(I==1)w.add(I);
else b.add(I);
}
int j=0;
for(int k=0;j<a.size()&&k<r.size();k++,j++)a.set(j,r.get(k));
for(int k=0;j<a.size()&&k<w.size();k++,j++)a.set(j,w.get(k));
for(int k=0;j<a.size()&&k<b.size();k++,j++)a.set(j,b.get(k));
}
}