0

I am attempting to utilize radix sort in order to sort a list of unordered integers, both positive and negative. I have the ability to sort a list of positive numbers, but I am confused on how to use radix sort with negative numbers. I was wondering if someone could help me out with coding and explaining the part about how radix sort works with negative numbers. After some googling I understand that you have to treat the negative sign as a special character but I'm still on the confused train. Below you can see the implementation I have of radix sort at the moment, taken from https://gist.github.com/rizkyabdilah/1740053.

def radix_sort(random_list):
len_random_list = len(random_list)
modulus = 10
div = 1
while True:
    # empty array, [[] for i in range(10)]
    new_list = [[], [], [], [], [], [], [], [], [], []]
    for value in random_list:
        least_digit = value % modulus
        least_digit /= div
        new_list[least_digit].append(value)
    modulus = modulus * 10
    div = div * 10

    if len(new_list[0]) == len_random_list:
        return new_list[0]

    random_list = []
    rd_list_append = random_list.append
    for x in new_list:
        for y in x:
            rd_list_append(y)

random_data = [13, 8, 1992, 31, 3, 1993, 1, 0, -1]
print radix_sort(random_data)

Thanks for helping!

Undo
  • 25,519
  • 37
  • 106
  • 129
terrabl
  • 743
  • 3
  • 8
  • 23
  • 1
    Look here: http://stackoverflow.com/questions/15306665/radix-sort-for-negative-integers – Edgar Rokjān Feb 14 '16 at 18:47
  • yeah that's what I looked at to figure out about the special character but I dont know how I would code that... – terrabl Feb 14 '16 at 18:47
  • As it's mentioned in the link above you can split initial list into the two lists: for negative integers and for positive. Sort these lists separately and then join them together. To sort negative integers just make them positive, sort them with your radix sort, invert result and make them negative again. – Edgar Rokjān Feb 14 '16 at 18:52
  • As said by Edgar Rokyan, in https://stackoverflow.com/questions/15306665/radix-sort-for-negative-integers/44965851#44965851 I have posted an optimal answer. Move on to bitshift and bitwise AND. Use radix 256 or 65536 instead of arbitrary radii. – ytoamn Jul 07 '17 at 10:15

1 Answers1

1

The simplest method is to complement the sign bit of the most significant field for the count (histogram) generation and for the indexing during the radix sort. Assuming near random data, the process is memory bandwidth limited due to the random writes, so the overhead of complementing the sign bit won't have much impact on overall performance.

This is assuming ones or twos complement integers.

If I remember correctly, sign + magnitude, which is how IEEE floating point values are effectively implemented (if radix sorting them as if they were integers), requires complementing the sign bit for positive numbers, and negation of the entire number for negative numbers.

rcgldr
  • 27,407
  • 3
  • 36
  • 61