Radix Sort, Sorting a float data

南楼画角 提交于 2020-01-19 07:55:12

问题


Is radix sort capable of sorting float data for example 0.5, 0.9, 1.02, etc.?


回答1:


Yes, it is possible. It requires an additional pass to correctly handle negative values. The articles by Pierre Terdiman and Michael Herf discuss in detail how to implement it. In short, you convert the float to unsigned integer, sort them, and then convert them back to float (this is required, otherwise the negatives values would be incorrectly sorted after the positive ones).

Their method has the advantage that you do not introduce any error into your data (provided that your processor stores the float in accordance to the IEEE 754 standard).




回答2:


Not out-of-the-box, but you have some options. You can discretize the data, e.g., by multiplying by 100 and rounding (so that you would have, for your example above, 5, 9, and 102). You could also bucketize the data (grouping numbers by ranges, as in 0 < x <= 1, 1 < x <= 2), and then sort within each bucket.



来源:https://stackoverflow.com/questions/4640906/radix-sort-sorting-a-float-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!