简体   繁体   中英

radix sort in c on floating points numbers

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!

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

Have a look at this :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM