Skip to content

Commit eb24272

Browse files
authored
create countSort.js
1 parent a782cf4 commit eb24272

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Algorithms/countSort.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let countingSort = (arr) => {
2+
let i = 0,
3+
j = 0,
4+
len = arr.length,
5+
count = [];
6+
let max = Math.min;
7+
for(let i = 0; i<arr.length; i++){
8+
if(arr[i] > max) max = arr[i];
9+
}
10+
for (i; i <= max; i++) {
11+
count[i] = 0;
12+
}
13+
for (i = 0; i < len; i++) {
14+
count[arr[i]] += 1;
15+
}
16+
for (i = 0; i <= max; i++) {
17+
while (count[i] > 0) {
18+
arr[j] = i;
19+
j++;
20+
count[i]--;
21+
}
22+
}
23+
return arr;
24+
};
25+

0 commit comments

Comments
 (0)