Digit-by-digit distribution sorting
Radix Sort sorts numbers by processing individual digits. By using a stable sorting subroutine (like Counting Sort) for each digit, it avoids comparisons and achieves linear time complexity for fixed-size integers.
Press "Start Sorting" to begin
Least Significant Digit (LSD) Radix sort processes numbers starting from the units place and moves towards the most significant digit.
Elements are placed into 10 buckets (for base-10) based on their current digit's value.
Crucially, elements are collected from the buckets in FIFO order. This preserves the relative sorting done in previous digit passes.
Where $n$ is the number of elements, $k$ is the range of digits (10 for decimal), and $d$ is the number of digits in the largest number.
1for digit d from 0 to maxDigits:2 buckets = [ [] for 10 ]3 for x in arr:4 digit = getDigit(x, d)5 buckets[digit].append(x)6 7 arr = flatten(buckets)1def radix_sort(arr):2 max_val = max(arr)3 exp = 14 while max_val // exp > 0:5 counting_sort_by_digit(arr, exp)6 exp *= 107 8def counting_sort_by_digit(arr, exp):9 n = len(arr)10 output = [0] * n11 count = [0] * 1012 13 for x in arr:14 idx = (x // exp) % 1015 count[idx] += 116 17 for i in range(1, 10):18 count[i] += count[i-1]19 20 for i in range(n-1, -1, -1):21 idx = (arr[i] // exp) % 1022 output[count[idx] - 1] = arr[i]23 count[idx] -= 124 25 for i in range(n):26 arr[i] = output[i]1function radixSort(arr) {2 const max = Math.max(...arr);3 4 for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {5 let buckets = Array.from({ length: 10 }, () => []);6 7 for (let num of arr) {8 const digit = Math.floor(num / exp) % 10;9 buckets[digit].push(num);10 }11 12 arr = [].concat(...buckets);13 }14 15 return arr;16}