-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradixsort.py
48 lines (40 loc) · 1.16 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
def radix_sort(arr):
# Find the maximum number to determine the number of digits
max_num = max(arr)
# Perform counting sort for every digit
exp = 1
while max_num // exp > 0:
counting_sort(arr, exp)
exp *= 10
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
# Store the count of each digit in count[]
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
# Change count[i] so that count[i] contains the actual position
# of this digit in output[]
for i in range(1, 10):
count[i] += count[i - 1]
# Build the output array
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
# Copy the output array to arr[] so that arr[] contains sorted numbers
for i in range(n):
arr[i] = output[i]
# Enter user inputs in an array
n = int(input("Enter the number of elements: "))
arr = []
for i in range(n):
a = int(input("Enter element: "))
arr.append(a)
print()
print("Unsorted Array:", arr)
radix_sort(arr)
print("Sorted array:", arr)