Radix Sort is a special kind of sorting algorithm that works very differently from comparison-based sorting methods like Bubble Sort or Quick Sort. Instead of comparing numbers with each other, Radix Sort sorts numbers digit by digit. It usually starts from the least significant digit, such as the units place, and moves toward the most significant digit. Because of this unique approach, Radix Sort can be extremely fast for certain types of data.
with hands-on learning.
get the skills and confidence to land your next move.
Radix Sort matters because it performs very well when sorting large lists of integers with a fixed number of digits. It is commonly used in systems that need high performance, such as database indexing, digital processing systems, and low-level software tools. Learning how to implement Radix Sort in TypeScript helps beginners understand non-comparison sorting, bucket logic, and how numbers can be processed step by step in a smart way.
Program 1: Basic Radix Sort for Positive Integers
This program shows the standard and most commonly taught version of Radix Sort. It works only with positive integers and sorts them digit by digit using counting sort logic internally.
function getMax(arr: number[]): number {
return Math.max(...arr);
}
function countingSort(arr: number[], exp: number): void {
let output: number[] = new Array(arr.length).fill(0);
let count: number[] = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
let index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
let index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr: number[]): number[] {
let max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
return arr;
}
let numbers: number[] = [170, 45, 75, 90, 802, 24, 2, 66];
console.log("Sorted array:", radixSort(numbers));This program works by sorting the numbers based on each digit, starting from the units place and moving left. Beginners can think of it as sorting by one digit at a time until the entire number is in the correct position. It is useful because it avoids direct comparisons and performs very well for large datasets of integers.
Program 2: Radix Sort Using Clear Helper Functions
This version breaks the logic into smaller helper functions to make the code easier to read and understand. It is especially helpful for beginners who prefer clear structure.
function radixSortSimple(arr: number[]): number[] {
let maxDigits = Math.max(...arr).toString().length;
for (let k = 0; k < maxDigits; k++) {
let buckets: number[][] = Array.from({ length: 10 }, () => []);
for (let i = 0; i < arr.length; i++) {
let digit = Math.floor(arr[i] / Math.pow(10, k)) % 10;
buckets[digit].push(arr[i]);
}
arr = ([] as number[]).concat(...buckets);
}
return arr;
}
let data: number[] = [329, 457, 657, 839, 436, 720, 355];
console.log("Sorted array:", radixSortSimple(data));This program uses buckets to group numbers by digits at each position. Beginners can visualize numbers being placed into buckets and then collected back together. This makes Radix Sort easier to understand conceptually.
Program 3: Radix Sort Using While Loop Control
This program uses a while loop instead of a fixed digit count. It continues sorting until all digit places are processed.
function radixSortWhile(arr: number[]): number[] {
let exp = 1;
let max = Math.max(...arr);
while (Math.floor(max / exp) > 0) {
let buckets: number[][] = Array.from({ length: 10 }, () => []);
for (let num of arr) {
let digit = Math.floor(num / exp) % 10;
buckets[digit].push(num);
}
arr = ([] as number[]).concat(...buckets);
exp *= 10;
}
return arr;
}
let values: number[] = [91, 46, 85, 15, 92, 35];
console.log("Sorted array:", radixSortWhile(values));This version shows how loop control can be dynamic instead of fixed. Beginners learn how Radix Sort adapts based on the size of the numbers, which makes the algorithm feel more flexible and intelligent.
Program 4: Radix Sort for Fixed-Length Numbers
This program assumes all numbers have the same number of digits. It simplifies the logic and is useful for special cases.
function radixSortFixed(arr: number[]): number[] {
for (let exp = 1; exp <= 100; exp *= 10) {
let buckets: number[][] = Array.from({ length: 10 }, () => []);
for (let num of arr) {
let digit = Math.floor(num / exp) % 10;
buckets[digit].push(num);
}
arr = ([] as number[]).concat(...buckets);
}
return arr;
}
let fixedNumbers: number[] = [123, 321, 213, 312];
console.log("Sorted array:", radixSortFixed(fixedNumbers));This approach is useful when working with known formats, such as IDs or codes with fixed length. Beginners can see how assumptions about data can simplify code.
Program 5: Radix Sort Using Functional Style
This program uses a more modern and clean coding style while still keeping the logic beginner-friendly.
function radixSortFunctional(arr: number[]): number[] {
let maxDigits = Math.max(...arr).toString().length;
for (let i = 0; i < maxDigits; i++) {
arr = arr.reduce((buckets: number[][], num) => {
let digit = Math.floor(num / Math.pow(10, i)) % 10;
buckets[digit].push(num);
return buckets;
}, Array.from({ length: 10 }, () => []))
.flat();
}
return arr;
}
let nums: number[] = [88, 410, 1772, 20, 5];
console.log("Sorted array:", radixSortFunctional(nums));This version helps beginners get comfortable with modern JavaScript and TypeScript features. It shows that Radix Sort logic can be written in different styles while keeping the same behavior.
Program 6: Radix Sort with Large Numbers
This program demonstrates Radix Sort working with very large integers.
function getMax(arr: number[]): number {
return Math.max(...arr);
}
function countingSort(arr: number[], exp: number): void {
let output: number[] = new Array(arr.length).fill(0);
let count: number[] = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
let index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
let index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr: number[]): number[] {
let max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
return arr;
}
let largeNumbers: number[] = [12345, 54321, 67890, 98765, 11111];
console.log("Sorted array:", radixSort(largeNumbers));Beginners can see that Radix Sort handles large values easily as long as they are integers. This makes it suitable for real-world numeric data.
Program 7: Radix Sort with Duplicate Values
This program shows Radix Sort handling duplicate numbers correctly.
function getMax(arr: number[]): number {
return Math.max(...arr);
}
function countingSort(arr: number[], exp: number): void {
let output: number[] = new Array(arr.length).fill(0);
let count: number[] = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
let index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
let index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr: number[]): number[] {
let max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
return arr;
}
let duplicateNumbers: number[] = [5, 3, 5, 2, 8, 3, 1];
console.log("Sorted array:", radixSort(duplicateNumbers));Radix Sort is stable, meaning it keeps the relative order of equal elements. Beginners can trust it for data where duplicates matter.
Program 8: Radix Sort for Already Sorted Data
This program sorts data that is already in order.
function getMax(arr: number[]): number {
return Math.max(...arr);
}
function countingSort(arr: number[], exp: number): void {
let output: number[] = new Array(arr.length).fill(0);
let count: number[] = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
let index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
let index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr: number[]): number[] {
let max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
return arr;
}
let sortedNumbers: number[] = [10, 20, 30, 40, 50];
console.log("Sorted array:", radixSort(sortedNumbers));This example shows that Radix Sort still works correctly even when no changes are needed. Beginners learn that algorithms should handle all cases gracefully.
Program 9: Radix Sort Supporting Negative Numbers
By default, Radix Sort does not handle negative numbers. This program tweaks the logic to support them by separating positive and negative values.
function getMax(arr: number[]): number {
return Math.max(...arr);
}
function countingSort(arr: number[], exp: number): void {
let output: number[] = new Array(arr.length).fill(0);
let count: number[] = new Array(10).fill(0);
for (let i = 0; i < arr.length; i++) {
let index = Math.floor(arr[i] / exp) % 10;
count[index]++;
}
for (let i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
let index = Math.floor(arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (let i = 0; i < arr.length; i++) {
arr[i] = output[i];
}
}
function radixSort(arr: number[]): number[] {
let max = getMax(arr);
for (let exp = 1; Math.floor(max / exp) > 0; exp *= 10) {
countingSort(arr, exp);
}
return arr;
}
function radixSortWithNegatives(arr: number[]): number[] {
let positives = arr.filter(n => n >= 0);
let negatives = arr.filter(n => n < 0).map(n => Math.abs(n));
positives = radixSort(positives);
negatives = radixSort(negatives).reverse().map(n => -n);
return negatives.concat(positives);
}
let mixedNumbers: number[] = [170, -45, 75, -90, 802, 24, -2, 66];
console.log("Sorted array:", radixSortWithNegatives(mixedNumbers));This program shows how Radix Sort can be extended to support negative values. Beginners learn that algorithms can be adapted to handle limitations with a little creativity.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Radix Sort in TypeScript in a simple and friendly way.
Q1. Why is Radix Sort faster than comparison-based sorting?
Radix Sort avoids comparing values directly and instead processes digits, which can be much faster for large datasets.
Q2. Is Radix Sort used in real applications?
Yes, Radix Sort is used in databases, digital systems, and performance-critical software.
Q3. Does Radix Sort work with floating point numbers?
Radix Sort works best with integers. Floating point numbers usually need special handling or conversion.
Q4. Is Radix Sort stable?
Yes, Radix Sort is stable, meaning it keeps the order of equal elements.
Q5. What should I learn after Radix Sort?
After Radix Sort, beginners often explore Counting Sort, Bucket Sort, and algorithm optimization techniques.
Conclusion
Radix Sort is a unique and powerful sorting algorithm that shows there is more than one way to sort data. In this article, we explored many TypeScript programs that implement Radix Sort using different styles, loops, and extensions for negative numbers. Each example showed how flexible and efficient Radix Sort can be.
If you are learning TypeScript or algorithms, try running these programs and modifying the input values. Practice will help you understand digit-based sorting more deeply. Keep experimenting, keep learning, and enjoy growing your programming skills.




