5

好的,所以我必须为无符号整数和浮点数创建一个基数排序。我的无符号整数版本可以正常工作,但我在让它用于浮点值时遇到了一些麻烦。基本上它按浮点数的整数值对我的数组的值进行排序,但它不会根据十进制值对其进行排序。(例如 36.65234 将出现在 36.02311 之前,如果它在未排序的数组中排在第一位)这个代码段是我进行位操作和屏蔽的地方,我很确定这是我的问题所在。

/* 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;
  }

提前致谢!

4

1 回答 1

5

基数排序是一种线性排序算法。它可以应用于floating point值。

看看这个:

于 2011-03-01T01:04:32.917 回答