Search code examples
sortingbinaryradix

Radix sort for two's complement binary numbers


I have a question about Radix Sort implementations. For 16-bit two's complement numbers in binary how would a Radix Sort work? I'm not entirely sure how an implementation would be constructed (possibly because I have a hard time doing two's complement conversions...). Does anyone have an explanation or a tutorial?


Solution

  • Just partition the numbers into positive and negative subsets using the sign bit. Then apply radix sort in each set. Both set will be sorted separately, in the same order (ascending/descending). Then concatenate them as required.