Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit a3c53ab

Browse files
Merge pull request #74 from ItsOKayCZ/add-radix-sort
Adding Radix sort | Sorting Algorithm | Python
2 parents f54adef + 2954710 commit a3c53ab

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class RadixSort:
2+
""" RadixSort Algoritm Implementation in Python 3.0+
3+
4+
arr: Unorded list
5+
output: Return list in ascending order.
6+
time complexity: O(kn)
7+
8+
Exmaple:
9+
>>> sort = RadixSort()
10+
>>> sort([5, 2, 1, 6, 10])
11+
[1, 2, 5, 6, 10]"""
12+
13+
def __init__(self):
14+
print('Radix sort algorithm is initialized');
15+
16+
def __call__(self, arr):
17+
maxLength = False
18+
tmp = -1
19+
placement = 1
20+
21+
while(not maxLength):
22+
maxLength = True
23+
buckets = [list() for _ in range(10)]
24+
25+
for i in arr:
26+
tmp = int(i / placement)
27+
buckets[tmp % 10].append(i)
28+
if(maxLength and tmp > 0):
29+
maxLength = False
30+
a = 0
31+
for b in range(10):
32+
buck = buckets[b]
33+
for i in buck:
34+
arr[a] = i
35+
a += 1
36+
placement *= 10
37+
38+
return arr
39+
40+
sort = RadixSort()
41+
print(sort([5, 2, 1, 6, 10]))

0 commit comments

Comments
 (0)