Skip to content

radix__sort.py #12157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions sorts/radix__sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Python program for implementation of Radix Sort

Check failure on line 1 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:1:50: W291 Trailing whitespace

# A function to do counting sort of arr[] according to

Check failure on line 3 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:3:55: W291 Trailing whitespace
# the digit represented by exp.

Check failure on line 4 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:4:32: W291 Trailing whitespace
def counting_sort(arr, exp1):

Check failure on line 5 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:5:30: W291 Trailing whitespace
n = len(arr)

Check failure on line 6 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W191)

sorts/radix__sort.py:6:1: W191 Indentation contains tabs

Check failure on line 6 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:6:14: W291 Trailing whitespace

# The output array elements that will have sorted arr

Check failure on line 8 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W191)

sorts/radix__sort.py:8:1: W191 Indentation contains tabs

Check failure on line 8 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:8:55: W291 Trailing whitespace
output = [0] * (n)

Check failure on line 9 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W191)

sorts/radix__sort.py:9:1: W191 Indentation contains tabs

Check failure on line 9 in sorts/radix__sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

sorts/radix__sort.py:9:20: W291 Trailing whitespace

# initialize count array as 0
count = [0] * (10)

# Store count of occurrences in count[]
for i in range(0, n):
index = (arr[i]/exp1)
count[int((index)%10)] += 1

# Change count[i] so that count[i] now contains actual
# position of this digit in output array
for i in range(1,10):
count[i] += count[i-1]

# Build the output array
i = n-1
while i>=0:
index = (arr[i]/exp1)
output[ count[ int((index)%10) ] - 1] = arr[i]
count[int((index)%10)] -= 1
i -= 1

# Copying the output array to arr[],
# so that arr now contains sorted numbers
i = 0
for i in range(0,len(arr)):
arr[i] = output[i]

# Method to do Radix Sort
def radix_sort(arr):

# Find the maximum number to know number of digits
max1 = max(arr)

# Do counting sort for every digit. Note that instead
# of passing digit number, exp is passed. exp is 10^i
# where i is current digit number
exp = 1
while max1 // exp > 0:
counting_sort(arr,exp)
exp *= 10

# Driver code to test above
arr = [ 170, 45, 75, 90, 802, 24, 2, 66]
radix_sort(arr)

for i in range(len(arr)):
print(arr[i],end=" ")