14

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

moinudin
  • 117,949
  • 42
  • 185
  • 213
BGV
  • 201
  • 3
  • 5
  • I would like to implement radix sorting by reducing its buckets into 0 and 1 only meaning I would convert every input into its binary value and then proceed to radix sort, would this be an option to speed up its sorting or this would make radix sort a little bit slower than before?. Thanks. – BGV Jan 12 '11 at 18:00
  • What you are describing is a "binary radix sort". For 32 bit `float` data, it will do 32 passes. 2**32 is ~4 billion, so unless you have that much data, a O(N lgN) sort like mergesort will likely be faster. (for 64 bit `double`s, the limit is 18 billion billion) – AShelly Feb 22 '19 at 23:31

2 Answers2

26

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).

moinudin
  • 117,949
  • 42
  • 185
  • 213
Sylvain Defresne
  • 38,469
  • 11
  • 70
  • 82
  • 1
    Here is another interesting article ( http://seven-degrees-of-freedom.blogspot.com/2010/07/question-of-sorts.html ) comparing radix sort to an SPU parallelized version of merge sort. In summary the merge sort while more complex (the complexity is O(n log n) agains the O(n) of radix sort), can be more easily parallelized and win in the end. – Sylvain Defresne Jan 09 '11 at 19:24
1

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.

EmeryBerger
  • 3,735
  • 15
  • 29