Counting sort and radix sort put integers in order without ever comparing two of them. Counting sort tallies how many times each value occurs, then reads the tallies back from low to high; radix sort applies that same counting pass one decimal digit at a time, starting from the least significant. Because they skip comparisons, both run in linear time on the right inputs — under the O(n log n) floor that any comparison sort is bound by. You will implement both on one object, countingRadixSort = { countingSort, radixSort }. See counting sort and radix sort for background.
countingRadixSort.countingSort(arr) // integers (may be negative) -> new sorted array
countingRadixSort.radixSort(arr) // non-negative integers -> new sorted array
Both return a brand-new array and leave the input untouched.
countingRadixSort.countingSort([4, 2, 7, 1]); // [1, 2, 4, 7]
countingRadixSort.countingSort([-2, 5, -9, 0]); // [-9, -2, 0, 5]
countingRadixSort.countingSort([3, 1, 3, 1, 3]); // [1, 1, 3, 3, 3]
countingRadixSort.radixSort([170, 45, 75, 90, 802, 24, 2, 66]);
// [2, 24, 45, 66, 75, 90, 170, 802]
countingRadixSort.radixSort([1, 1000, 10, 100]); // [1, 10, 100, 1000]
countingRadixSort.radixSort([5, -3, 1]); // throws TypeError
O(n + range). Great for [0, 100]; ruinous for [0, 1e9].0.TypeError if a negative value slips in.arr.sort(); the whole point is to sort by counting, not by comparing.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Counting sort and radix sort put integers in order without ever comparing two of them. Counting sort tallies how many times each value occurs, then reads the tallies back from low to high; radix sort applies that same counting pass one decimal digit at a time, starting from the least significant. Because they skip comparisons, both run in linear time on the right inputs — under the O(n log n) floor that any comparison sort is bound by. You will implement both on one object, countingRadixSort = { countingSort, radixSort }. See counting sort and radix sort for background.
countingRadixSort.countingSort(arr) // integers (may be negative) -> new sorted array
countingRadixSort.radixSort(arr) // non-negative integers -> new sorted array
Both return a brand-new array and leave the input untouched.
countingRadixSort.countingSort([4, 2, 7, 1]); // [1, 2, 4, 7]
countingRadixSort.countingSort([-2, 5, -9, 0]); // [-9, -2, 0, 5]
countingRadixSort.countingSort([3, 1, 3, 1, 3]); // [1, 1, 3, 3, 3]
countingRadixSort.radixSort([170, 45, 75, 90, 802, 24, 2, 66]);
// [2, 24, 45, 66, 75, 90, 170, 802]
countingRadixSort.radixSort([1, 1000, 10, 100]); // [1, 10, 100, 1000]
countingRadixSort.radixSort([5, -3, 1]); // throws TypeError
O(n + range). Great for [0, 100]; ruinous for [0, 1e9].0.TypeError if a negative value slips in.arr.sort(); the whole point is to sort by counting, not by comparing.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.