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!