Radix Sort – Data Structures and Algorithms Tutorials Medium 0 attempts
GeeksforGeeks ↗

Radix Sort – Data Structures and Algorithms Tutorials

Medium Sorting&Searching GeeksforGeeks

Implement Radix Sort algorithm to sort an array of non-negative integers.

Example: arr = [170, 45, 75, 90, 802, 24, 2, 66] → [2, 24, 45, 66, 75, 90, 170, 802]

Sample Input
Sample Output
Constraints
  • 1 <= N <= 10^5
  • 0 <= arr[i] <= 10^9

LSD Radix Sort

function radixSort(arr) {
  const max = Math.max(...arr);
  for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
    const buckets = Array.from({length:10}, ()=>[]);
    for (const n of arr) buckets[Math.floor(n/exp) % 10].push(n);
    arr = buckets.flat();
  }
  return arr;
}

Time: O(d × (n + k)) | Space: O(n + k)

Saved in this browser only. Private to you.

JavaScript