mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
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:
@ -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]
|
||||
|
Reference in New Issue
Block a user