Skip to content

Commit a1feca8

Browse files
authored
Fixed empty file
1 parent b807a62 commit a1feca8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎SortingAlgorithms/countingSort.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let countingSort = (arr, min, max) => {
2+
const count = [];
3+
const sortedArr = [];
4+
5+
for (let i = min; i <= max; i++) {
6+
count[i] = 0;
7+
}
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
count[arr[i]] += 1;
11+
}
12+
13+
for (let i = 1; i < arr.length; i++) {
14+
count[i] += count[i-1];
15+
}
16+
17+
//Loop over the input array in reverse to have a stable sort
18+
for (let i = arr.length - 1; i >= 0; i--) {
19+
sortedArr[count[arr[i]]-1] = arr[i];
20+
count[arr[i]]--;
21+
}
22+
23+
return sortedArr;
24+
};

0 commit comments

Comments
 (0)