Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2943,8 +2943,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- JavaScript -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/javascript/CountingSort.js">
<img align="center" height="25" src="./logos/javascript.svg" />
</a>
</td>
<td> <!-- Swift -->
Expand Down
31 changes: 31 additions & 0 deletions src/javascript/CountingSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function countingSort(arr) {
// Find the maximum element in the array to determine the range
let max = Math.max(...arr);

// Create a count array to store the count of each element
let count = new Array(max + 1).fill(0);

// Count the occurrences of each element in the input array
for (let i = 0; i < arr.length; i++) {
count[arr[i]]++;
}

// Modify the count array to store the actual position of each element
for (let i = 1; i <= max; i++) {
count[i] += count[i - 1];
}

// Create a new output array and store the elements in their sorted order
let output = new Array(arr.length);
for (let i = arr.length - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}

return output;
}

const arr = [4, 2, 2, 8, 3, 3, 1];
const sortedArr = countingSort(arr);
console.log("Original array:", arr);
console.log("Sorted array:", sortedArr);