forked from alicijabulota/new_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.py
80 lines (60 loc) · 1.84 KB
/
radix_sort.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import timeit
import random
def r_sort(alist, base=10):
def list_to_buckets(alist, base, iteration):
buckets = [[] for i in range(base)]
for number in alist:
digit = (number // (base ** iteration)) % base
buckets[digit].append(number)
return buckets
def buckets_to_list(buckets):
alist = []
for bucket in buckets:
alist.extend(bucket)
return alist
max_val = max(alist)
it = 0
while ((base ** it) <= max_val):
alist = buckets_to_list(list_to_buckets(alist, base, it))
it += 1
return alist
def string_radix(arr, base=128):
try:
longest = find_longest_string(arr)
except IndexError:
return arr
index_num = 0
index_place = (longest - 1)
buckets = [[] for x in range(base)]
while index_place >= 0:
for val in arr:
try:
index_num = ord(val[index_place])
except IndexError:
index_num = 0
buckets[index_num].append(val)
transfer_list = []
for bucket in buckets:
for i in range(len(bucket)):
transfer_list.append(bucket.pop(0))
arr = transfer_list
index_place -= 1
return arr
def find_longest_string(arr):
longest = max(map(len, arr))
return longest
if __name__ == '__main__':
def easy_sort():
x = range(1000)
r_sort(x)
def hard_sort():
x = random.sample(range(1000), 1000)
r_sort(x)
print("This is sorting a range of 1000")
print(timeit.Timer("easy_sort()",
setup="from __main__ import easy_sort")
.timeit(number=1000))
print("This is sorting a random range 1-1000")
print(timeit.Timer("hard_sort()",
setup="from __main__ import hard_sort")
.timeit(number=1000))