radix sort in c on floating points numbers

9.1k Views Asked by At

Okay so I have to create a radix sort for both unsigned ints and floating point numbers. My unsigned ints version works as it should, I am having a little trouble getting it to work for floating points values though. Basically it sorts the values of my array by the whole number value of the floating point number but it doesn't sort it based on the decimal value. (Ex. 36.65234 will appear before 36.02311 if it comes first in the unsorted array) This code segment is where I do my bit manipulations and masking, which I am pretty sure is where my problem is.

/* For loop to create bin */
for(int i=0; i<n; i++){
    temp_int = (((unsigned int)(list[i]))>>bitwise)&0xff;
    bin[temp_int] = bin[temp_int]+1;
  }

  /*For loop to get map */
  for (int i=0; i<256; i++) {
    map[i+1] = bin[i]+count;
    count = map[i+1];
  }

  /* For loop to copy "sorted" values into other array */
  for (int i=0; i<n; i++) {
    temp_int = (((unsigned int)(list[i]))>>bitwise)&0xff;
    int buf_loc = map[temp_int];
    temp_arr[buf_loc] = list[i];
    map[temp_int] = map[temp_int]+1;
  }

Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

Radix sort is a linear sorting algorithm. It can be applied to floating point values.

Have a look at this :