Skip to content

Commit faed847

Browse files
committed
Updated the count sort algorithm with stable implementation. Added ES6 syntactic sugar to the syntax. Removed the unwanted len variable and updated the comments
1 parent 6232f8d commit faed847

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Sorts/CountingSort.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
const countingSort = (arr, min, max) => {
1212
// Create an auxiliary resultant array
1313
const res = []
14-
// Create the freq array
15-
let len = max - min + 1
16-
const count = new Array(len).fill(0)
14+
// Create and initialize the frequency[count] array
15+
const count = new Array(max - min + 1).fill(0)
1716
// Populate the freq array
1817
for (let i = 0; i < arr.length; i++) {
19-
count[arr[i] - min] += 1
18+
count[arr[i] - min]++
2019
}
21-
// Create a prefix sum array out of the freq array
20+
// Create a prefix sum array out of the frequency[count] array
2221
count[0] -= 1
2322
for (let i = 1; i < count.length; i++) {
2423
count[i] += count[i - 1]

0 commit comments

Comments
 (0)