Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSASortingRadix SortAlgorithms

Radix Sort — Algorithm Visualized

Learn radix sort — digit-by-digit stable counting sort, LSD passes, O(d(n+10)) complexity, interactive multi-pass visualizer.

Jul 4, 20263 min read

Introduction

Radix sort sorts integers digit by digit (LSD: least significant first) using stable counting sort on each digit position. Handles large value ranges where plain counting sort would need too much memory.

Quick index

#Section
1How it works
2Interactive visualizer
3Full implementation
4Complexity analysis
5Real-life examples

1. How it works

plaintext
Pass 1: sort by ones digit   → stable
Pass 2: sort by tens digit   → stable
...
Final array sorted

Stability at each pass preserves ordering from previous passes.

Back to index


2. Interactive visualizer

Radix Sort — Step Visualizer

Stable counting sort on each digit — ones, tens, hundreds — LSD first.

Step 1 / 9 — Unsorted

Unsorted

170
45
75
90
802
24
2
66

Step 1 — Input array

const arr = [170, 45, 75, ...]
// sort by each digit LSD → MSD

Radix sort uses stable counting sort on each digit position.

Time

O(d(n + 10))

Space

O(n + 10)

Stable

Yes

Tip

After pass 1 (ones digit), numbers are not fully sorted — but digit positions processed so far are correct relative to later passes.

Back to index


3. Full implementation

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

Function calls with output

js
// ── Demo calls — output shown on the right ──
const arr = [170, 45, 75, 90, 802, 24, 2, 66]
 
radixSort(arr) // → [2, 24, 45, 66, 75, 90, 170, 802]

Back to index


4. Complexity analysis

MetricValue
TimeO(d × (n + 10)) — d = digits
SpaceO(n + 10) per pass
StableYes

Interview Answer

"Why must each digit pass be stable?"

Stability preserves the order from previous digit passes — without it, higher-digit ordering from earlier passes would be destroyed.

Back to index


5. Real-life examples

ScenarioWhy radix sort
Sorting phone numbersFixed 10 digits — sort by each digit position
Student ID sortingIDs like 2024001234 — LSD radix on decimal digits
IP address sortingSort by octets — same digit-by-digit idea
Zip code batch processing5-digit codes — d=5 passes, linear per pass
Date sorting (YYYYMMDD)Integer dates — radix sort without string parsing

Employee ID sorting

10-digit employee IDs — LSD radix sort processes each digit position.

802
170
45
90
66

Index → value

HR batch — unsorted 3-digit IDs (demo scale).

Real-world implementation

function countingSortByDigit(arr, exp) {
  const out = Array(arr.length).fill(0)
  const count = Array(10).fill(0)
  for (const n of arr) count[Math.floor(n / exp) % 10]++
  for (let i = 1; i < 10; i++) count[i] += count[i - 1]
  for (let i = arr.length - 1; i >= 0; i--) {
    const d = Math.floor(arr[i] / exp) % 10
    out[--count[d]] = arr[i]
  }
  return out
}

function radixSort(arr) {
  let a = [...arr]
  const max = Math.max(...a)
  for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
    a = countingSortByDigit(a, exp)
  }
  return a
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const ids = [802, 170, 45, 90, 66]

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Radix sort extends counting sort to large integers by processing fixed-width digits — linear in n for bounded d.

Next reads: Counting Sort · Quick Sort

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime