-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathradix.py
More file actions
36 lines (27 loc) · 742 Bytes
/
radix.py
File metadata and controls
36 lines (27 loc) · 742 Bytes
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
from math import log10
from random import randint
def get_digit(number, base, pos):
return (number // base ** pos) % base
def prefix_sum(array):
for i in range(1, len(array)):
array[i] = array[i] + array[i-1]
return array
def radixsort(l, base=10):
passes = int(log10(max(l))+1)
output = [0] * len(l)
for pos in range(passes):
count = [0] * base
for i in l:
digit = get_digit(i, base, pos)
count[digit] +=1
count = prefix_sum(count)
for i in reversed(l):
digit = get_digit(i, base, pos)
count[digit] -= 1
new_pos = count[digit]
output[new_pos] = i
l = list(output)
return output
l = [ randint(1, 99999) for x in range(100) ]
sorted = radixsort(l)
print sorted