From faed847317aae6ed061200377741538f30f38371 Mon Sep 17 00:00:00 2001 From: Mandy8055 Date: Sun, 8 Aug 2021 17:21:05 +0530 Subject: [PATCH] Updated the count sort algorithm with stable implementation. Added ES6 syntactic sugar to the syntax. Removed the unwanted len variable and updated the comments --- Sorts/CountingSort.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sorts/CountingSort.js b/Sorts/CountingSort.js index 86389b720..a31ea4455 100644 --- a/Sorts/CountingSort.js +++ b/Sorts/CountingSort.js @@ -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]