Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit cf86fdf

Browse files
authored
Merge pull request #10 from Ditobaskoro/countingSort
Counting sort
2 parents ab053bf + 6fc0149 commit cf86fdf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sorting/countingSort.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// JavaScript implementation of counting sort
2+
//
3+
// Author: Dito Baskoro
4+
5+
function countingSort(arr, min, max)
6+
{
7+
let i, z = 0, count = [];
8+
9+
for (i = min; i <= max; i++) {
10+
count[i] = 0;
11+
}
12+
13+
for (i=0; i < arr.length; i++) {
14+
count[arr[i]]++;
15+
}
16+
17+
for (i = min; i <= max; i++) {
18+
while (count[i]-- > 0) {
19+
arr[z++] = i;
20+
}
21+
}
22+
return arr;
23+
}
24+
25+
//Test
26+
let array = [2, 0, 3, 5, 4, 1];
27+
console.log(array.length);
28+
console.log("Original Array");
29+
console.log(array);
30+
console.log("Sorted Array");
31+
console.log(countingSort(array, 0, 5));

0 commit comments

Comments
 (0)