4

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!

mike
  • 2,849
  • 6
  • 28
  • 33
  • Thanks herbalessence, I saw some of the other examples the first link you posted answered my question, I am just wondering can anybody give an explanation on what this line of code achieves? temp_int = (*(int *)&temp_arr[i])>>offset&0xff; where temp_arr holds an array of floats, and offset is the number of bits I want to offset (Ex. my grouping is 8 bits so offset increments from 0->8->16->24) – mike Mar 01 '11 at 01:35

1 Answers1

5

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

Have a look at this :

Community
  • 1
  • 1
sgokhales
  • 49,762
  • 33
  • 123
  • 158