-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadixSort.py
65 lines (52 loc) · 1.74 KB
/
RadixSort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# counting sort that has a map with only <radix> keys
def countingSourt(array, exp, radix=10):
maximum = max(array)
minimum = min(array)
counts = [0] * radix
result = [0] * len(array)
for value in array:
digit = (value // exp) % radix
counts[digit] += 1
# Make counts[] directly store the position of each value in array
for ii in range(len(counts) - 1):
counts[ii+1] += counts[ii]
# result will be the sorted list
for value in reversed(array):
digit = (value // exp) % radix
index = counts[digit] - 1
result[index] = value
counts[digit] -= 1
# The outer array will know nothing about the rebinding in the method, so don't write it this way
# array = result
for idx, value in enumerate(result):
array[idx] = value
def radixSort(array, radix=10):
# Use the maimum value in array to get maximum number of digits
maximum = max(array)
count = 0
while maximum > 0:
count += 1
maximum = maximum // radix
tempArray = array
minimum = min(array)
if minimum < 0:
# Shift values to make input array all positive
tempArray = [x + (-minimum) for x in array]
else:
minimum = 0
exp = 1
for ii in range(count):
countingSourt(tempArray, exp, radix)
exp *= radix
# Shift value back
for idx, value in enumerate(tempArray):
array[idx] = value + minimum
if __name__ == '__main__':
# array1 = [8, 6, 3, 7, 60, 3, 60, 3, 212, 103, 113]
array1 = [8, 6, 3, 7, 60, 3, 60, -12, 3, 212, -1, 103, -11, 113]
# array2 = array1
print("Original array:")
print(array1)
radixSort(array1)
print("Sorted array (increasing, unstable):")
print(array1)