Updated the count sort algorithm with stable implementation. Added ES6 syntactic sugar to the syntax. Removed the unwanted len variable and updated the comments

This commit is contained in:
Mandy8055
2021-08-08 17:21:05 +05:30
parent 6232f8dd42
commit faed847317

View File

@ -11,14 +11,13 @@
const countingSort = (arr, min, max) => {
// Create an auxiliary resultant array
const res = []
// Create the freq array
let len = max - min + 1
const count = new Array(len).fill(0)
// Create and initialize the frequency[count] array
const count = new Array(max - min + 1).fill(0)
// Populate the freq array
for (let i = 0; i < arr.length; i++) {
count[arr[i] - min] += 1
count[arr[i] - min]++
}
// Create a prefix sum array out of the freq array
// Create a prefix sum array out of the frequency[count] array
count[0] -= 1
for (let i = 1; i < count.length; i++) {
count[i] += count[i - 1]