📖 Scenario: You work in a warehouse where boxes are labeled with numbers. You want to organize these boxes in order from smallest to largest number quickly and efficiently.
🎯 Goal: Build a program that sorts a list of numbers using the Radix Sort algorithm, which sorts numbers digit by digit from the least significant digit to the most significant digit.
📋 What You'll Learn
Create an array called numbers with the exact values: [170, 45, 75, 90, 802, 24, 2, 66]
Create a variable called maxDigits to store the number of digits in the largest number
Write a function called radixSort that sorts the numbers array using Radix Sort
Print the sorted array after applying Radix Sort
💡 Why This Matters
🌍 Real World
Radix Sort is used in systems where sorting large sets of numbers quickly is important, like organizing warehouse inventory or processing large datasets.
💼 Career
Understanding Radix Sort helps in roles involving data processing, optimization, and algorithm design, which are common in software engineering and data science jobs.
Progress0 / 4 steps
1
Create the initial array of numbers
Create an array called numbers with these exact values: [170, 45, 75, 90, 802, 24, 2, 66]
DSA Javascript
Hint
Use const numbers = [...] to create the array with the exact numbers.
2
Find the maximum number of digits
Create a variable called maxDigits that stores the number of digits in the largest number in the numbers array
DSA Javascript
Hint
Use Math.max(...numbers) to find the largest number, then convert it to string and get its length.
3
Write the Radix Sort function
Write a function called radixSort that takes the numbers array and sorts it using Radix Sort algorithm by processing digits from least significant to most significant
DSA Javascript
Hint
Use a loop from 0 to maxDigits - 1. Create 10 buckets for digits 0-9. Place numbers in buckets based on current digit. Flatten buckets back to array. Return sorted array.
4
Print the sorted array
Call the radixSort function with the numbers array and print the sorted result using console.log
DSA Javascript
Hint
Call radixSort(numbers) and print the result with console.log.
Practice
(1/5)
1. What is the main idea behind the Radix Sort Algorithm?
easy
A. Sort numbers by swapping adjacent elements repeatedly
B. Sort numbers by comparing each pair directly
C. Sort numbers by dividing them into two halves recursively
D. Sort numbers digit by digit starting from the least significant digit
Solution
Step 1: Understand Radix Sort approach
Radix Sort sorts numbers by processing each digit separately, starting from the least significant digit (rightmost).
Step 2: Compare with other sorting methods
Unlike comparison-based sorts, Radix Sort groups numbers by digits without direct comparisons between whole numbers.
Final Answer:
Sort numbers digit by digit starting from the least significant digit -> Option D
Quick Check:
Radix Sort = Digit by digit sorting [OK]
Hint: Radix Sort processes digits from right to left [OK]
We need 10 separate arrays (buckets) for digits 0-9. Using Array.from with a function creates independent arrays.
Step 2: Why other options fail
const buckets = new Array(10).fill([]); fills with the same array reference, causing all buckets to share one array. const buckets = [].map(() => []); fails because map on empty array does nothing. const buckets = Array(10).fill(null); fills with null, not arrays.
4. Identify the bug in this Radix Sort snippet and choose the correct fix:
function radixSort(arr) {
let maxDigit = Math.floor(Math.log10(Math.max(...arr))) + 1;
for (let k = 0; k <= maxDigit; k++) {
let buckets = Array.from({ length: 10 }, () => []);
for (let num of arr) {
buckets[Math.floor(num / Math.pow(10, k)) % 10].push(num);
}
arr = [].concat(...buckets);
}
return arr;
}
medium
A. Change loop condition to k < maxDigit instead of <= maxDigit
B. Initialize buckets with new Array(10).fill([])
C. Use Math.ceil instead of Math.floor for maxDigit
D. Remove Math.abs from digit calculation
Solution
Step 1: Analyze loop range for digit passes
The loop runs from 0 to maxDigit inclusive, causing one extra pass beyond needed digits.
Step 2: Fix loop condition
Changing k <= maxDigit to k < maxDigit ensures exactly maxDigit passes, avoiding unnecessary iteration.
Final Answer:
Change loop condition to k < maxDigit instead of <= maxDigit -> Option A
Quick Check:
Loop runs maxDigit times, not maxDigit+1 [OK]
Hint: Loop digits from 0 to less than maxDigit [OK]
Common Mistakes:
Using <= causes extra unnecessary pass
Initializing buckets incorrectly with shared arrays
Changing floor to ceil breaks digit count
5. You want to use Radix Sort to sort an array containing both positive and negative integers, like [170, -45, 75, -90, 802, 24, -2, 66]. Which approach correctly adapts Radix Sort for this case?
hard
A. Separate negative and positive numbers, sort positives with Radix Sort, sort negatives by absolute value then reverse, then combine
B. Apply Radix Sort directly on all numbers including negatives without changes
C. Convert all numbers to strings and sort lexicographically
D. Use Radix Sort only on positive numbers and ignore negatives
Solution
Step 1: Understand Radix Sort limitation
Radix Sort works only on non-negative integers because it sorts digits from least significant to most significant.
Step 2: Adapt for negatives
Separate negatives and positives. Sort positives normally. For negatives, sort their absolute values, then reverse the result to get correct order. Finally, combine negatives (in reversed order) before positives.
Final Answer:
Separate negative and positive numbers, sort positives with Radix Sort, sort negatives by absolute value then reverse, then combine -> Option A
Quick Check:
Separate negatives and positives, sort separately [OK]
Hint: Sort negatives by absolute then reverse, positives normally [OK]
Common Mistakes:
Trying to sort negatives directly with Radix Sort
Ignoring negative numbers in sorting
Sorting numbers as strings causing wrong order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.