mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +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) => {
|
const countingSort = (arr, min, max) => {
|
||||||
// Create an auxiliary resultant array
|
// Create an auxiliary resultant array
|
||||||
const res = []
|
const res = []
|
||||||
// Create the freq array
|
// Create and initialize the frequency[count] array
|
||||||
let len = max - min + 1
|
const count = new Array(max - min + 1).fill(0)
|
||||||
const count = new Array(len).fill(0)
|
|
||||||
// Populate the freq array
|
// Populate the freq array
|
||||||
for (let i = 0; i < arr.length; i++) {
|
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
|
count[0] -= 1
|
||||||
for (let i = 1; i < count.length; i++) {
|
for (let i = 1; i < count.length; i++) {
|
||||||
count[i] += count[i - 1]
|
count[i] += count[i - 1]
|
||||||
|
Reference in New Issue
Block a user