Hello. I have three questions about the following code;
static void funct(int[] list) {
final int N = 20;
java.util.ArrayList[] buckets = new java.util.ArrayList[N];
for(int i = 0; i< list.length; i++) {
int key = list[i];
if(buckets[key] = null)
buckets[key].add(list[i]);
}
int k = 0
for(int i = 0; i <buckets.length; i++) {
if(buckets[i] != null) {
for(int j = 0; j< buckets[i].size(); j++)
list[k++] = (Integer)buckets[i].get(j);
}
}
}
The algorithm has a major shortcoming, is it that it will only work for up to 20 elements and that it has a poor complexity?
The point of the code is to sort a list of elements - they are put into an array, then put into buckets, then they are put from the buckets into the array again?
Heres the one I'm stumped on, how would you modify the method so that you could pass an array which contains objects of another class instead of integers?