Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 4 · syllabusAsked 2× in KTU papersChecked by code · teacher review pending

Radix sort

Sort digit by digit, least significant first, using stable buckets.

Syllabus says: “Radix Sort”

On this page · 16 sections
ormakkurippu ✎
Never-forget card

Sort by the LAST digit first (LSD), then tens, then hundreds — each pass must be STABLE. No comparisons at all.

Picture it

The post office sorting letters by PIN code: first into piles by the last digit, then re-pile by the next digit, keeping order within each pile.

Write this in the exam

LSD radix sort sorts the keys digit by digit from the least significant to the most significant digit, using a stable bucket distribution in each pass.

Learning objectives

  • Perform LSD radix sort pass by pass with buckets 0–9.
  • Explain why the per-pass distribution must be stable.
  • State the O(d(n + r)) complexity.

Definition

Definition

Radix sort (least significant digit first) makes d passes, where d is the number of digits in the largest key. In each pass, keys are distributed into r = 10 queues by the current digit and collected from queue 0 to queue 9.

Why it works

After pass k, the keys are sorted by their last k digits. The next pass sorts by digit k+1; because the distribution keeps the previous order within each bucket, ties on digit k+1 stay sorted by the lower digits.

Worked example

Worked example: 170, 45, 75, 90, 802, 24, 2, 66checked by code
Input170 45 75 90 802 24 2 66
After pass 1 (units)170 90 802 2 24 45 75 66
After pass 2 (tens)802 2 24 45 66 170 75 90
After pass 3 (hundreds)2 24 45 66 75 90 170 802

Totals: passes = 3

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

Algorithm / pseudocode

Radix sort (LSD)
  1. 1RadixSort(a, n) d ← digits in max(a)
  2. 2for place ← 1, 10, 100, … (d passes)
  3. 3for each x in a (left to right): bucket[⌊x/place⌋ mod 10].enqueue(x)
  4. 4a ← bucket[0] ++ bucket[1] ++ … ++ bucket[9]
  5. 5end

C implementation

radix_sort.ctested ✓ 132/132 cases
C
/* Radix sort (LSD, base 10, bucket queues) — KTU PCCST303, Module 4
 * Works for non-negative integers.
 * Input : n followed by n integers
 * Output: the array after every digit pass, then the sorted array.  */
#include <stdio.h>

#define MAX 100

void printArray(const char *label, int a[], int n) {
    printf("%s:", label);
    for (int i = 0; i < n; i++) printf(" %d", a[i]);
    printf("\n");
}

void radixSort(int a[], int n) {
    int bucket[10][MAX], count[10];
    int max = 0;
    for (int i = 0; i < n; i++) if (a[i] > max) max = a[i];
    int passes = 1;
    for (int m = max; m >= 10; m /= 10) passes++;   /* digits in max */

    for (int p = 0, place = 1; p < passes; p++, place *= 10) {
        for (int b = 0; b < 10; b++) count[b] = 0;
        for (int i = 0; i < n; i++) {                /* distribute    */
            int d = (a[i] / place) % 10;
            bucket[d][count[d]++] = a[i];
        }
        int k = 0;
        for (int b = 0; b < 10; b++)                 /* collect 0..9  */
            for (int i = 0; i < count[b]; i++) a[k++] = bucket[b][i];
        printArray("PASS", a, n);
    }
}

int main(void) {
    int n, a[MAX];
    if (scanf("%d", &n) != 1 || n < 0 || n > MAX) { printf("Invalid n\n"); return 1; }
    for (int i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1 || a[i] < 0) { printf("Invalid input\n"); return 1; }
    }
    radixSort(a, n);
    printArray("SORTED", a, n);
    return 0;
}
Sample run · KTU PYQ input · real output from gcc

Input (stdin)

7 341 22 33 122 42 5 4

Output (stdout)

PASS: 341 22 122 42 33 4 5
PASS: 4 5 22 122 33 341 42
PASS: 4 5 22 33 42 122 341
SORTED: 4 5 22 33 42 122 341

Open in the Code Lab →

Complexity

Each pass is O(n + r). With d passes: O(d·(n + r)). For keys with a fixed number of digits this is linear in n. Extra space O(n + r) for the buckets.

Advantages and disadvantages

AdvantagesDisadvantages
No key comparisons; linear for fixed-width keysNeeds extra memory for buckets
StableOnly for keys that can be split into digits (integers, fixed-length strings)

Edge cases

  • Keys with fewer digits are treated as having leading zeros (5 = 005).
  • Negative numbers need special handling; the simulator rejects them rather than giving a wrong answer.
  • If max = 0 or there is a single key, one pass is still made.

Common mistakes

  • Starting with the most significant digit while still using LSD collection.
  • Collecting a bucket in reverse order (breaks stability).
  • Doing too few passes — count the digits of the largest key.

Asked in KTU exams (2)

Quick check — can you get them all?

Question 1

Q1.Radix sort of n keys with d digits in base r takes…

Question 2

Q2.Why must each pass be stable?

Question 3

Q3.After pass 1 (units) on 121, 3, 44, 65, 342, 21, 34 the first three are…

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • d passes, units → tens → hundreds.
  • Distribute into 10 FIFO buckets, collect 0 → 9.
  • O(d(n + 10)), stable, not comparison-based.

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 7 (Sorting), Ch. 8 (Hashing) §7.8
  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — Ch. 2, 6, 7, 8, 11 §8.3

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)