Skip to content

Commit 4e1b1a8

Browse files
authored
Create radix_sort.py
1 parent e4ca730 commit 4e1b1a8

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Sorting/Radix Sort/radix_sort.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Problem Name: Radix Sort Algorithm
2+
# Problem Statement: Given an unsorted array of elements (here integers),
3+
# the task is to perform the Radix Sort algorithm to
4+
# sort the elements in the ascending order.
5+
6+
# ------------------------------------------------------------------------------
7+
8+
# Constraints:
9+
# arr[] -> array of unsorted elements.
10+
# countingSort(arr, exp1) -> implementation of Counting Sort before Radix Sort.
11+
# radixSort(arr) -> implementation of Radix sort.
12+
13+
# ------------------------------------------------------------------------------
14+
15+
# Python program for implementation of Radix Sort
16+
# A function to do counting sort of arr[] according to
17+
# the digit represented by exp.
18+
19+
def countingSort(arr, exp1):
20+
21+
n = len(arr)
22+
23+
# The output array elements that will have sorted arr
24+
output = [0] * (n)
25+
26+
# initialize count array as 0
27+
count = [0] * (10)
28+
29+
# Store count of occurrences in count[]
30+
for i in range(0, n):
31+
index = arr[i] // exp1
32+
count[index % 10] += 1
33+
34+
# Change count[i] so that count[i] now contains actual
35+
# position of this digit in output array
36+
for i in range(1, 10):
37+
count[i] += count[i - 1]
38+
39+
# Build the output array
40+
i = n - 1
41+
while i >= 0:
42+
index = arr[i] // exp1
43+
output[count[index % 10] - 1] = arr[i]
44+
count[index % 10] -= 1
45+
i -= 1
46+
47+
# Copying the output array to arr[],
48+
# so that arr now contains sorted numbers
49+
i = 0
50+
for i in range(0, len(arr)):
51+
arr[i] = output[i]
52+
53+
# Method to do Radix Sort
54+
def radixSort(arr):
55+
56+
# Find the maximum number to know number of digits
57+
max1 = max(arr)
58+
59+
# Do counting sort for every digit. Note that instead
60+
# of passing digit number, exp is passed. exp is 10^i
61+
# where i is current digit number
62+
exp = 1
63+
while max1 / exp >= 1:
64+
countingSort(arr, exp)
65+
exp *= 10
66+
67+
68+
# Driver code
69+
arr = [170, 45, 75, 90, 802, 24, 2, 66]
70+
71+
print ("-- Implementation of Radix Sort Algorithm --")
72+
print ()
73+
print ("Array before implementing the Radix Sort...")
74+
print (" ".join(str(k) for k in arr))
75+
print ()
76+
print ("Radix Sort going on...")
77+
print ()
78+
print ("After implementing Radix Sort algorithm...")
79+
80+
# Function Call
81+
radixSort(arr)
82+
print (" ".join(str(k) for k in arr))
83+
84+
# ------------------------------------------------------------------------------
85+
86+
# Output:
87+
# -- Implementation of Radix Sort Algorithm --
88+
89+
# Array before implementing the Radix Sort...
90+
# 170 45 75 90 802 24 2 66
91+
92+
# Radix Sort going on...
93+
94+
# After implementing Radix Sort algorithm...
95+
# 2 24 45 66 75 90 170 802
96+
97+
# ------------------------------------------------------------------------------
98+
99+
# Code contributed by, Abhishek Sharma, 2022
100+
101+
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)