From 75466e885a692a17bee1b50a1c21537950d14a75 Mon Sep 17 00:00:00 2001 From: Ankur-S1 Date: Tue, 5 Oct 2021 18:38:23 +0530 Subject: [PATCH 1/3] Count sort --- CountingSort/CountSort.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 CountingSort/CountSort.cpp diff --git a/CountingSort/CountSort.cpp b/CountingSort/CountSort.cpp new file mode 100644 index 0000000..8c47f95 --- /dev/null +++ b/CountingSort/CountSort.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +#define ln "\n"; +#define TC() int t; cin >> t; while(t--) +#define IO() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) + +int main(){ + + vector arr = {1,4,1,2,7,5,2}; + int max = *max_element(arr.begin(), arr.end()); + int min = *min_element(arr.begin(), arr.end()); + int range = max - min + 1; + + vector count(range), output(arr.size()); + for (int i = 0; i < arr.size(); i++) + count[arr[i] - min]++; + + for (int i = 1; i < count.size(); i++) + count[i] += count[i - 1]; + + for (int i = arr.size() - 1; i >= 0; i--) { + output[count[arr[i] - min] - 1] = arr[i]; + count[arr[i] - min]--; + } + + for (int i = 0; i < arr.size(); i++) + arr[i] = output[i]; + + for(auto i : arr) cout << i << " "; + + return 0; +} From 7b67521a6c199a190281d67ad2d2b8e4c622da49 Mon Sep 17 00:00:00 2001 From: Ankur-S1 Date: Tue, 5 Oct 2021 18:42:28 +0530 Subject: [PATCH 2/3] Added readme --- CountingSort/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 CountingSort/README.md diff --git a/CountingSort/README.md b/CountingSort/README.md new file mode 100644 index 0000000..4355f07 --- /dev/null +++ b/CountingSort/README.md @@ -0,0 +1,2 @@ +Counting sort is a sorting method which is used to sort the given array in linear time. It works like hashing technique, +where it counts the number of objects having distinct key values and does some prefix sum calculations and calculates the position of each object in the output sequence. From 6b929b7686a8c96eefa2da02d768606afe7d66f1 Mon Sep 17 00:00:00 2001 From: Ankur-S1 Date: Tue, 5 Oct 2021 18:43:00 +0530 Subject: [PATCH 3/3] Update README.md --- CountingSort/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CountingSort/README.md b/CountingSort/README.md index 4355f07..89f9c56 100644 --- a/CountingSort/README.md +++ b/CountingSort/README.md @@ -1,2 +1,2 @@ Counting sort is a sorting method which is used to sort the given array in linear time. It works like hashing technique, -where it counts the number of objects having distinct key values and does some prefix sum calculations and calculates the position of each object in the output sequence. +where it counts the number of objects having distinct key values and does some prefix sum calculations and finds the position of each object in the output sequence.