Radix Sort Implementation in JavaScript
Radix sort is a non-comparative sorting algorithm that sorts data by processing individual digits. It's particularly efficient for sorting integers or strings with a fixed number of digits/characters. Your task is to implement radix sort in JavaScript to efficiently sort an array of non-negative integers.
Problem Description
You are required to implement the radix sort algorithm in JavaScript. The algorithm should take an array of non-negative integers as input and return a new array containing the same integers sorted in ascending order. The implementation should handle arrays of varying lengths and should be efficient in terms of time complexity. The algorithm should work by iterating through the digits of the numbers from least significant to most significant, using a stable sorting algorithm (like counting sort) to sort the array based on each digit.
Key Requirements:
- Non-Negative Integers: The input array will contain only non-negative integers.
- Stable Sorting: The counting sort used within radix sort must be stable. A stable sort preserves the relative order of equal elements.
- Least Significant Digit First: The sorting process should start with the least significant digit and move towards the most significant digit.
- New Array: The function should return a new sorted array, not modify the original array in place.
Expected Behavior:
The function should take an array of non-negative integers and return a new array containing the same integers sorted in ascending order. If the input array is empty, it should return an empty array.
Edge Cases to Consider:
- Empty input array.
- Array with a single element.
- Array with all elements being the same.
- Arrays with numbers having different numbers of digits.
Examples
Example 1:
Input: [170, 45, 75, 90, 802, 24, 2, 66]
Output: [2, 24, 45, 66, 75, 90, 170, 802]
Explanation: Radix sort processes the digits from right to left. First, it sorts based on the ones place, then the tens place, and finally the hundreds place, resulting in a sorted array.
Example 2:
Input: [121, 432, 564, 23, 1, 45, 788]
Output: [1, 23, 45, 121, 432, 564, 788]
Explanation: The algorithm correctly sorts the array based on the digits, handling numbers with varying lengths.
Example 3: (Edge Case)
Input: []
Output: []
Explanation: An empty array is returned as input.
Constraints
- The input array will contain only non-negative integers.
- The maximum value of any integer in the input array will be less than 10,000 (i.e., up to 4 digits).
- The length of the input array will be between 0 and 1000.
- The time complexity of your solution should be O(nk), where n is the number of elements in the array and k is the maximum number of digits in any element.
Notes
- Consider using the
counting sortalgorithm as a subroutine within your radix sort implementation. Remember that counting sort needs to be stable. - Determine the maximum number of digits in the input array to know how many passes of counting sort are needed.
- Think about how to extract the digit at a specific place value (e.g., the ones place, the tens place) from a number. The modulo operator (%) and integer division (Math.floor()) can be helpful.
- Focus on clarity and readability in your code. Well-commented code is appreciated.