mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 06:52:59 +08:00
23 lines
431 B
JavaScript
23 lines
431 B
JavaScript
let countingSort = (arr, min, max) => {
|
|
let i = min,
|
|
j = 0,
|
|
len = arr.length,
|
|
count = [];
|
|
for (i; i <= max; i++) {
|
|
count[i] = 0;
|
|
}
|
|
for (i = 0; i < len; i++) {
|
|
count[arr[i]] += 1;
|
|
}
|
|
for (i = min; i <= max; i++) {
|
|
while (count[i] > 0) {
|
|
arr[j] = i;
|
|
j++;
|
|
count[i]--;
|
|
}
|
|
}
|
|
return arr;
|
|
};
|
|
|
|
|