-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadixSort.py
35 lines (28 loc) · 1018 Bytes
/
RadixSort.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
def count_sort(array,length,position):
array_out=[0]*length
#counting array is fixed size 10 as digits at each place are always between 0 & 9
count_list=[0]*10
#Filling count array w.r.t position
for i in array:
count_list[(i//position)%10]+=1 #for taking elements position by position
#Updating count array to give actual index
for i in range(1,len(count_list)):
count_list[i]=count_list[i]+count_list[i-1]
#Filling sorted items in second array
for i in reversed(range(0,length)):
index=(array[i]//position)%10
array_out[count_list[index]-1]=array[i]
count_list[index]-=1
#Copying items into original array
for i in range(0,length):
array[i]=array_out[i]
del array_out
return array
def radixSort(array):
length=len(array)
max_val=max(array)
position=1
while (max_val//position) > 0:
array=count_sort(array,length,position)
position*=10
return array