2
$\begingroup$

A few months ago I learned about the magic that allows radix sort to run in O(n) time and space. Most tutorials on radix sort say it is useful for very large collections where the key values fall inside a defined range. They then go on to say that it's really only reasonable when sorting unsigned integers. The reasons made sense.

I also recently learned about the IEEE floating point specification and it got me thinking about about 2s-compliment signed ints at the same time. In both cases the MSB of the value represents the sign of the value, with 1 indicating a negative and 0 indicating positive. That means that an int or float with a MSB of 1 should go before another with a MSB of 0.

With that in mind, couldn't signed ints or floats be sorted with the following algorithm?

  1. Run Radix Sort using the raw bytes. This takes O(n). Let p be the count of positive numbers in the array. The array now has two parts.
  • array[0:p] is a sorted array of the positive values.
  • array[p:n] is a reverse sorted array of the negative values.
  1. Initialize pointers i = 0; j = n - 1 and the final return array of length n.
  2. While msd(array[j])==1, copy array[j] to the final array and decrement j. This takes O(n-p)
  3. While i < j, copy array[i] to the final array and increment i. This takes O(p)
  4. The final array is now sorted.

If this works it runs in O(n) + O(n-p) + O(p) = O(n) time. We can reuse the second array from the radix sort, which used O(n) space.

This feels like a relatively easy way to extend Radix sort to signed values. Does it not work? What am I missing?

$\endgroup$
2
  • $\begingroup$ Did you try to google that? I know that you can use radix sort for signed ints by operating on bits of n+0x80...0, i.e. converting signed int into unsigned one before each operation, although it should be easier just to convert data before and after the sort. Radix sort is also used for floats, although I don't know the algo. Google it $\endgroup$
    – Bulat
    Commented Dec 11, 2021 at 18:22
  • $\begingroup$ Moreover, you can use MSD Radix sort for faster sorting of variable-length data, such as strings. In particular, it's implemented in Boost.Sort for C++. All Radix sort needs is a function mapping value to a bucket, with the property that all values from bucket i are smaller than data in bucket i+1 $\endgroup$
    – Bulat
    Commented Dec 11, 2021 at 18:30

1 Answer 1

1
$\begingroup$

Radixsort for signed integers is probably considered a folklore result. Take a look at Exercises 8 and 9 of Section 5.2.5 of Knuth's book The Art of Computer Programming: Volume 3: Sorting and Searching: Sorting and Searching, Volume 3.

$\endgroup$
2
  • $\begingroup$ Sorry, what does a "folklore result" mean? Could you explain in other words? $\endgroup$
    – Dan Getz
    Commented Sep 13 at 19:44
  • $\begingroup$ A mathematical result is called folklore if it is unpublished and has no clear originator but is well-circulated and believed to be true among specialists. $\endgroup$
    – zouzias
    Commented Sep 14 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.