Sorting floating-point values using their byte-representation

自古美人都是妖i 提交于 2019-12-04 05:22:21

Your approach:

I think a simple modification can extend this to negative numbers: XOR all positive numbers with 0x8000... and negative numbers with 0xffff.... This should flip the sign bit on both (so negative numbers go first), and then reverse the ordering on negative numbers. Does anyone see a problem with this?

is definitely the answer. Moreover, it was used, for example, in dBase and clones to organize sorting on a float column, and I guess it's followed by newer DB generations.

Also, it is identical to the "total order" according to IEEE-754 for binary representations. (But not for decimal ones, the latter is much more complex.)

UPDATE: as suggested by @Sneftel: you could find replacing -0 with +0 as useful before converting to bit string.

If you want to let Radix sorting stay a stable sorting algorithm, you have to swap all subsections of equal elemts in the negative section once more, because when you swaped the negative numbers, the original stable sorting were in stable order.

Assoc. prof. Arne Maus, Univ of Oslo

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