A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Radix Sort Algorithm

College Depth 83 in the knowledge graph ☐ I know this ☆ Set as goal
7topics build on this
400prerequisites beneath it
See this on the map →
Counting Sort AlgorithmAlgorithm Design Basics+1 more→→Insertion Sort AlgorithmLinear-Time Sorting: Counting Sort and Radix Sort
sorting radix-sort linear-time non-comparison digit-by-digit stable

Core Idea

Radix sort treats numbers as sequences of digits and sorts them digit-by-digit using a stable sub-sort (like counting sort). Processing digits from least-significant to most-significant yields O(d * (n + b)) time, where d is the number of digits and b is the base. For fixed-length integers, this is linear in input size and faster than comparison sorts in practice.

How It's Best Learned

Trace radix sort on small numbers, processing one digit at a time. Implement using counting sort as the stable sub-sort. Understand why least-significant-digit processing works. Compare performance to quicksort and mergesort on large integer arrays.

Common Misconceptions

Explainer

You already know from counting sort that comparison-based sorting has a lower bound of O(n log n), but counting sort beats this by exploiting the structure of the keys — it counts occurrences rather than comparing elements. The limitation of counting sort is that it needs an array as large as the range of values, so sorting 32-bit integers directly would require an array of 4 billion entries. Radix sort solves this by applying counting sort one digit at a time, keeping the auxiliary array small while still achieving linear-time performance.

The key insight is processing digits from least significant to most significant (LSD radix sort). This seems backwards at first — wouldn't you want to sort by the most important digit first? The reason LSD ordering works is that counting sort is stable: elements with equal keys maintain their original relative order. When you sort by the ones digit, ties are left in their input order. When you then sort by the tens digit, elements with the same tens digit remain sorted by their ones digit (because of stability). Each pass refines the ordering without disturbing the work of previous passes. After processing all d digits, the array is fully sorted.

Consider sorting the numbers [329, 457, 657, 839, 436, 720, 355]. In base 10, the first pass sorts by the ones digit: 720, 355, 436, 457, 657, 329, 839. The second pass sorts by the tens digit: 720, 329, 436, 839, 355, 457, 657. The third pass sorts by the hundreds digit: 329, 355, 436, 457, 657, 720, 839. Each pass is a counting sort over just 10 buckets (digits 0–9), processing all n elements in O(n + 10) = O(n) time. With d = 3 passes, total work is O(3n) = O(n) for fixed-length keys.

The general complexity is O(d × (n + b)), where d is the number of digit positions and b is the base (number of possible digit values). Choosing the base is a design decision: a larger base reduces d (fewer passes) but increases the size of the counting array. For 32-bit integers, using base 256 means d = 4 passes with a 256-entry counting array — four linear scans over the data, which is often faster than quicksort's O(n log n) comparisons for large n. The tradeoff is that radix sort requires O(n + b) extra space for the counting sort output, making it less memory-efficient than in-place comparison sorts. Radix sort excels when keys are fixed-length integers or strings and the number of digit positions is small relative to log n — exactly the scenarios where its linear time complexity provides a real advantage over comparison-based alternatives.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Counting Sort Algorithm → Radix Sort Algorithm

Longest path: 84 steps · 400 total prerequisite topics

Prerequisites (3)

Leads To (2)