I was given a problem for one of my CS courses where I have to program a LSD radix sort that can sort unsigned integers (+ or -). It is given that the values to be sorted are 32-bit integer values.
The stipulation is that my mask must be a constant value, which is where my question lies. If I am doing an & bitwise operation on a 32-bit integer where each digit is represented by 4 bits (hexadecimal representation) should my mask be 28? (since I would like there to be 28 bits of 1's in binary)
Also if anyone could notices any additional errors, could you please bring attention to them?
#define BITS_PER_PASS 4
#define NUM_PASSES 8
#define NUM_BUCKETS 16
#define MASK 28
int *buckets[NUM_BUCKETS];
int bucket_sizes[NUM_BUCKETS];
void radix_sort( int *values, int n )
{
int i, j;
int bucket_index;
int *p;
for( i=0; i < NUM_PASSES; i++ )
{
for( j=0; j < NUM_BUCKETS; j++ )
{
bucket_sizes[j]=0;
}
for( j=0; j < n; j++ )
{
bucket_index = (values[j] & MASK) >> BITS_PER_PASS*i; //QUESTION
buckets[j][ bucket_sizes[bucket_index]]=values[j];
bucket_sizes[bucket_index]++;
}
p = values;
for( j=0; j < NUM_BUCKETS; j++ )
{
memcpy((void *)p, (void *)buckets[j], sizeof(int)*bucket_sizes[j]);
p+=bucket_sizes[j];
}
}
}
I would also like to add that all of the defined constants and global variables are mandatory since I was told to use these in my radix sort.