-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.py
More file actions
47 lines (38 loc) · 1.36 KB
/
RadixSort.py
File metadata and controls
47 lines (38 loc) · 1.36 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
###########################
# Author: Hemant Tripathi #
###########################
def main():
print('Starting program for Radix Sort')
dataArray = [25, 57, 48, 37, 12, 92, 86, 33]
maxDigit = getMax(dataArray)
print('Max Digit in Array: ', maxDigit)
exp = 1
count = 0
while(maxDigit//exp > 0): #double divide (//) will return int value, while single divide (/) will return float value
count = count+1
print("Pass : ", count)
radixSort(dataArray, exp)
exp = exp*10
def getMax(dataArray):
maxVal = dataArray[0]
for val in range(1, len(dataArray)):
if(maxVal < dataArray[val]):
maxVal = dataArray[val];
return maxVal
def radixSort(arr, exp):
count = [0,0,0,0,0,0,0,0,0,0]
output = [None]*len(arr)
for i in range(len(arr)): #calculating frequencies
count[(arr[i]//exp)%10]+=1
print('Frequencues of each indexes are : ', count)
for i in range(1,10): #calculate Cumulative frequencies
count[i] = count[i]+count[i-1]
print('Cumulative frequencies of each index are: ', count)
for i in range(len(arr)-1, -1, -1):
output[(count[(arr[i]//exp)%10])-1] = arr[i]
count[(arr[i]//exp)%10]-=1
for i in range(len(arr)):
arr[i] = output[i]
print('After this iteration, sorted array is: ', arr)
if __name__ == "__main__":
main()