-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.py
More file actions
59 lines (47 loc) · 1.55 KB
/
radix_sort.py
File metadata and controls
59 lines (47 loc) · 1.55 KB
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
import numpy as np
import random
'''
Running Time:
Given n d-digit numbers, each digit can take most k different values, radix_sort sorts the numbers in O(d(n+k)) time if the stable sort used takes
O(n+k) time.
Proof. We show by induction on i the invariant that the numbers are sorted on the least significant i digits.
The invariant is true for i = 1. Assume the invariant holds for i-1 >= 1 and we prove it for i.
The numbers are sorted on digit i. For the numbers with the same value on digit i, because a stable sort is used,
they are sorted on the least significant i-1 digits. Therefore, the invariant holds, implying radix sort sorts the numbers.
It takes O(n+k) time to sort on one digit, giving a total O(d(n+k)) time.
'''
def counting_sort(arr,k, d):
n = len(arr)
output = [0] * 10
count = [0] * k
for i in range(0, n):
index = arr[i] // d
count[index % 10] += 1
for i in range(1,k):
count[i] += count[i-1]
i = n-1
while i >= 0:
index = arr[i] // d
output[count[index%10]-1] = arr[i]
count[index%10] -= 1
i -= 1
for i in range(0, n):
arr[i] = output[i]
def radix_sort(arr, k):
max_num = max(arr)
d = 1
while max_num // d > 0:
counting_sort(arr,k, d)
d *= 10
def main():
arr = [0]*10
k = 20
for i in range(10):
arr[i] = random.randint(1,k)
print("Before Radix Sort")
print(arr)
print("After Radix Sort")
radix_sort(arr, k+1)
print(arr)
if __name__ == '__main__':
main()