Skip to content

Commit 146d9ab

Browse files
Merge pull request matthewsamuel95#623 from francamacdowell/add-radix_sort
Add radix sort python implementation
2 parents d210cd1 + d9e1594 commit 146d9ab

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def counting_sort(arr, exp1):
2+
3+
n = len(arr)
4+
5+
output = [0] * (n)
6+
7+
count = [0] * (10)
8+
9+
for i in range(0, n):
10+
index = (arr[i]/exp1)
11+
count[(index)%10] += 1
12+
13+
for i in range(1,10):
14+
count[i] += count[i-1]
15+
16+
i = n-1
17+
while i >= 0:
18+
index = arr[i]/exp1
19+
output[ count[(index)%10] - 1] = arr[i]
20+
count[(index)%10] -= 1
21+
i -= 1
22+
23+
i = 0
24+
for i in range(0,len(arr)):
25+
arr[i] = output[i]
26+
27+
def radix_sort(arr):
28+
29+
max1 = max(arr)
30+
31+
exp = 1
32+
while max1/exp > 0:
33+
countingSort(arr,exp)
34+
exp *= 10
35+
36+
radix_sort(arr)

0 commit comments

Comments
 (0)