C中浮点数的基数排序
好吧,我必须为无符号整数和浮点数创建基数排序。我的无符号整数版本可以正常工作,但我在使其适用于浮点值时遇到了一些麻烦。基本上,它按浮点数的整数值对数组的值进行排序,但不会根据十进制值对其进行排序。 (例如,如果 36.65234 在未排序数组中排在第一位,则 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;
}
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基数排序是一种线性排序算法。它可以应用于
浮点
值。看看这个:
Radix sort is a linear sorting algorithm. It can be applied to
floating point
values.Have a look at this :