Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create radix_sort.py
added python implementation of radix sort
  • Loading branch information
MrWeast committed Dec 1, 2023
commit 083f657b545e66582895ca074ea73e5a06f885f0
43 changes: 43 additions & 0 deletions src/python/radix_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def radix_sort(arr):
# Find the maximum number to know the number of digits
max_num = max(arr)

# Do counting sort for every digit, starting from the least significant 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

for i in range(n):
index = arr[i] // exp
count[index % 10] += 1

for i in range(1, 10):
count[i] += count[i - 1]

i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1

for i in range(n):
arr[i] = output[i]


def main():
arr = [170, 2, 45, 75, 75, 90, 802, 24, 2, 66]
print("Unsorted array:", arr)
radix_sort(arr)
print("Sorted array:", arr)


if __name__ == "__main__":
main()