I have been given some algorithms to reverse engineer. The algorithm below is a radix sort, but I am very confused about what is actually happening in the code.
I'm new to algorithms and am unsure how the code sorts elements in an array. I'm not sure what bits have to do with the algorithm and what a mask is. Here is the code:
ArrayList<Integer> array = CopyArray(a);
Integer[] zerobucket = new Integer[a.size()];
Integer[] onebucket = new Integer[a.size()];
int i, bit;
Integer element, mask;
for (bit=0; bit<8; ++bit) {
int zc = 0;
int oc = 0;
for(i=0; i<array.size(); ++i) {
element = array.get(i);
mask = 1 << bit;
if ((element & mask) == 0) {
zerobucket[zc++] = array.get(i);
} else {
onebucket[oc++] = array.get(i);
}
}
for(i=0; i<oc; ++i) array.set(i,onebucket[i]);
for(i=0; i<zc; ++i) array.set(i+oc,zerobucket[i]);
}
return(array);
Integer. They are slow and waste memory. Useint.