diff --git a/algorithms/Sorting/Counting_sort/README.md b/algorithms/Sorting/Counting_sort/README.md new file mode 100644 index 0000000..4617619 --- /dev/null +++ b/algorithms/Sorting/Counting_sort/README.md @@ -0,0 +1,11 @@ +## Implementation of Counting sort algorithm +### Complexity if max(O(n), O(max_value)) +''' +Input: +5 +9 4 6 21 3 +''' +''' +Output: +3 4 6 9 21 +''' \ No newline at end of file diff --git a/algorithms/Sorting/Counting_sort/counting_sort.cpp b/algorithms/Sorting/Counting_sort/counting_sort.cpp new file mode 100644 index 0000000..ff8b594 --- /dev/null +++ b/algorithms/Sorting/Counting_sort/counting_sort.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +// Complexeity is O(n + max_val) in case of integer sorting +void csort(int* a, int n){ + unordered_map hash; + int max_val = INT_MIN; + for(int i=0; imax_val)max_val = a[i]; + } + int j=0; + for(int i=0; i<=max_val; i++){ + while(hash[i]--){ + a[j]=i; + j++; + } + } +} +int main(){ + int n; + int* a; + cin>>n; + a = new int[n]; + for(int i=0; i>a[i]; + csort(a, n); + for(int i=0; i