Skip to main content

Radix Sort

Definition​

Radix Sort is a non-comparative sorting algorithm that sorts integers by grouping them based on individual digits or radix (base). It works by iterating through each digit of the numbers, starting from the least significant digit (rightmost) to the most significant digit (leftmost). Radix Sort can be implemented using different approaches like LSD (Least Significant Digit) or MSD (Most Significant Digit)

Practice​

radix_sort(arr):
max_digit = getMax(arr)
exp = 1
while max_digit // exp > 0:
counting_sort(arr, exp)
exp *= 10

counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

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

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

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

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

getMax(arr):
max_num = max(arr)
max_digit = len(str(max_num))
return max_digit