How it works
Sorts by processing digits from least significant to most significant, using counting sort as a subroutine.
Implementation
function radixSort(arr, stats) { const n = arr.length; if (n < 1) return; let max = arr[0]; for (let i = 1; i < n; i++) max = Math.max(max, arr[i]); const sorted = new Set(); for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) { const output = new Array(n); const count = new Array(10).fill(0); for (let i = 0; i < n; i++) { count[Math.floor(arr[i] / exp) % 10]++; } for (let i = 1; i < 10; i++) count[i] += count[i - 1]; for (let i = n - 1; i >= 0; i--) { const digit = Math.floor(arr[i] / exp) % 10; output[count[digit] - 1] = arr[i]; count[digit]--; } for (let i = 0; i < n; i++) { arr[i] = output[i]; write(i, output[i]); } checkpoint(exp, max); } for (let i = 0; i < n; i++) markSorted(i); }
def radixSort(arr, stats): if (n < 1): return max = arr[0] for i in range(1, n): max = (max if max >= arr[i] else arr[i]) sorted = set() exp = 1 while (int(max / exp) > 0): output = [0] * n count = [0] * 10 for i in range(n): count[(int(arr[i] / exp) % 10)] += 1 for i in range(1, 10): count[i] += count[(i - 1)] for i in range((n - 1), (0 - 1), -1): digit = (int(arr[i] / exp) % 10) output[(count[digit] - 1)] = arr[i] count[digit] -= 1 for i in range(n): arr[i] = output[i] write(i, output[i]) checkpoint(exp, max) exp *= 10 for i in range(n): markSorted(i)
#include <vector> #include <algorithm> void sort(std::vector<int>& arr, int n, int& comparisons, int& swaps) { if((n < 1)) { return; } int max = arr[0]; for(int i=1; i<n; i++) { max = ((max) > (arr[i]) ? (max) : (arr[i])); } int sorted = 0; for(int exp=1; ((max / exp) > 0); exp *= 10) { std::vector<int> output(n, 0); std::vector<int> count(10, 0); for(int i=0; i<n; i++) { count[((arr[i] / exp) % 10)]++; } for(int i=1; i<10; i++) { count[i] += count[(i - 1)]; } for(int i=(n - 1); i>(0 - 1); i--) { int digit = ((arr[i] / exp) % 10); output[(count[digit] - 1)] = arr[i]; count[digit]--; } for(int i=0; i<n; i++) { arr[i] = output[i]; write(i, output[i]); } checkpoint(exp, max); } for(int i=0; i<n; i++) { markSorted(i); } }
public void Sort(int[] arr, int n, dynamic stats) { if((n < 1)) { return; } int max = arr[0]; for(int i=1; i<n; i++) { max = Math.Max(max, arr[i]); } int sorted = 0; for(int exp=1; ((max / exp) > 0); exp *= 10) { int[] output = new int[n]; int[] count = new int[10]; for(int i=0; i<n; i++) { count[((arr[i] / exp) % 10)]++; } for(int i=1; i<10; i++) { count[i] += count[(i - 1)]; } for(int i=(n - 1); i>(0 - 1); i--) { int digit = ((arr[i] / exp) % 10); output[(count[digit] - 1)] = arr[i]; count[digit]--; } for(int i=0; i<n; i++) { arr[i] = output[i]; write(i, output[i]); } checkpoint(exp, max); } for(int i=0; i<n; i++) { markSorted(i); } }
#include <stdio.h> #include <string.h> #include <stdlib.h> void sort(int arr[], int n, int* comparisons, int* swaps) { if((n < 1)) { return; } int max = arr[0]; for(int i=1; i<n; i++) { max = ((max) > (arr[i]) ? (max) : (arr[i])); } int sorted = 0; for(int exp=1; ((max / exp) > 0); exp *= 10) { int* output = (int*)malloc((n) * sizeof(int)); int* count = (int*)malloc((10) * sizeof(int)); memset(count, 0, (10) * sizeof(int)); for(int i=0; i<n; i++) { count[((arr[i] / exp) % 10)]++; } for(int i=1; i<10; i++) { count[i] += count[(i - 1)]; } for(int i=(n - 1); i>(0 - 1); i--) { int digit = ((arr[i] / exp) % 10); output[(count[digit] - 1)] = arr[i]; count[digit]--; } for(int i=0; i<n; i++) { arr[i] = output[i]; write(i, output[i]); } checkpoint(exp, max); } for(int i=0; i<n; i++) { markSorted(i); } }