-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.py
More file actions
52 lines (46 loc) · 1.67 KB
/
radix_sort.py
File metadata and controls
52 lines (46 loc) · 1.67 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
import numpy as np
import math
def countingSort(num_seq, sorted_seq, size, shift):
"""
Counting sort algorithm.
Input: number sequence, empty sequence, large number in the number sequence
Complexity: O(k+n)
Counting sort algorithm is not a comparison sort, so it's complexity can beats
the lower bound of Omega(nlgn).
"""
cnt = [0] * size # this size may result in memory waste
for num in num_seq:
num = (num >> shift) & (size - 1)
cnt[num] += 1
for i in range(1, size):
cnt[i] += cnt[i - 1]
for num in reversed(range(len(num_seq))):
index = (num_seq[num] >> shift) & (size - 1)
sorted_seq[cnt[index] - 1] = num_seq[num]
cnt[index] -= 1
def radixSort(num_seq, sorted_seq, size, r):
"""
Radix sort algorithm.
Sorting number sequence from first r bits to last r bits of each element.
This algorithm must use a stable sort.
(i.e. order will not change after sorting e.g. counting sort)
Complexity: big-theta((b/r)(n+2^r))
Here we use r = lgn, so the complexity will be big-theta(bn/lgn)
"""
upper = 2**r
for i in range(size):
shift = i * r
countingSort(num_seq, sorted_seq, upper, shift)
num_seq = np.copy(sorted_seq)
if __name__ == '__main__':
upper, size = 100, 16
r = int(math.log2(size)) # choose r = lgn
b = upper.bit_length()
d = math.ceil(b / r) # sort d times in radix sort
sorted_seq = np.asarray([0] * size)
num_seq = np.random.randint(0, upper, size)
print('----before sorted----')
print(num_seq)
radixSort(num_seq, sorted_seq, d, r)
print('----after sorted----')
print(sorted_seq)