1. String Bucket Radix Sort (Brute Force)
Intuition
Find the maximum number of digits d. Pad all numbers with leading zeroes as strings. Starting from least significant digit to most significant digit, distribute numbers into 10 buckets (0 to 9) and collect them back. Repeat for all d digit places.
When to apply: Use to understand digit-by-digit distribution intuitively.
Iterative Selection Passes (O(N^2) Time)
2 of 2 questions left todaySelection sort places the absolute minimum at the start
Selection sort places the absolute minimum at the start.
import java.util.*; public class Main { public static class Solution { public int[] radixSort(int[] nums) { int max = 0; for (int x : nums) max = Math.max(max, x); for (int exp = 1; max / exp > 0; exp *= 10) { List<List<Integer>> buckets = new ArrayList<>(); for (int i = 0; i < 10; i++) buckets.add(new ArrayList<>()); for (int x : nums) { int digit = (x / exp) % 10; buckets.get(digit).add(x); } int idx = 0; for (List<Integer> b : buckets) { for (int x : b) nums[idx++] = x; } } return nums; } } public static void main(String[] args) { Solution sol = new Solution(); int[] nums = {170, 45, 75, 90, 802, 24, 2, 66}; System.out.println("Radix sorted: " + Arrays.toString(sol.radixSort(nums))); }}Time & Space Complexity
- Time complexity:
O(d * (n + b))— d passes where each pass places n items into 10 buckets. - Space complexity:
O(n)— O(n) auxiliary memory for bucket collections.
Why Brute Force Doesn't Scale:
Standard comparison sorts hit the theoretical Omega(N log N) decision tree lower bound. When integers have fixed word length (e.g. 32-bit integers, where D <= 4 with base 256), Radix Sort runs in strictly O(N) linear time using bitwise shift and mask (>> and & 0xFF).
For N = 10,000,000 32-bit integers: QuickSort requires ~240,000,000 operations; Base-256 Radix Sort (4 passes) requires ~40,000,000 operations (~6x faster).