Fill in the stable placement
Standard · Fill in the code · about 8 min
Complete counting sort for integers from 0 to k. The running totals mark where each key block ends, and the values are placed from right to left.
Fill the three blanks.
Example 1
Input:
[[3,1,2,1],3]
Output:
[1,1,2,3]
Example 2
Input:
[[2,5,3,0,2,3,0,3],5]
Output:
[0,0,2,2,3,3,3,5]
Constraints
- 0 ≤ nums.length ≤ 1,000
- 0 ≤ nums[i] ≤ k ≤ 100
- Return a new sorted array; do not use a library sort
Learn the idea first: Counting sort, a visual lesson.
More Counting sort problems
- Fill the count array Warm-up
- Count each key Warm-up
- Implement counting sort Standard
- Spot the bug: equal keys swap order Standard
- Sort by a bounded key, stably Challenge