From 9dd7b3cd013df73da405d68978a864f08884c02e Mon Sep 17 00:00:00 2001 From: Emmanuelveslin <41759142+Emmanuelveslin@users.noreply.github.com> Date: Sat, 5 Oct 2019 23:48:03 +0530 Subject: [PATCH] added counting sort repo --- countingsort.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 countingsort.js diff --git a/countingsort.js b/countingsort.js new file mode 100644 index 00000000..e74d63fd --- /dev/null +++ b/countingsort.js @@ -0,0 +1,22 @@ +let countingSort = (arr, min, max) => { + let i = min, + j = 0, + len = arr.length, + count = []; + for (i; i <= max; i++) { + count[i] = 0; + } + for (i = 0; i < len; i++) { + count[arr[i]] += 1; + } + for (i = min; i <= max; i++) { + while (count[i] > 0) { + arr[j] = i; + j++; + count[i]--; + } + } + return arr; +}; + +