diff --git a/sorting/README.md b/sorting/README.md index b7358847..514d5c1d 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -8,6 +8,8 @@ 4. [Merge Sort](c-or-cpp/merge-sort.c) 5. [Quick Sort](c-or-cpp/quick-sort.cpp) 6. [Heap Sort](c-or-cpp/heap-sort.cpp) +7. [Counting Sort](c-or-cpp/counting-sort.cpp) + ### C# diff --git a/sorting/c-or-cpp/counting-sort.cpp b/sorting/c-or-cpp/counting-sort.cpp new file mode 100644 index 00000000..e7f60fb9 --- /dev/null +++ b/sorting/c-or-cpp/counting-sort.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +//function to find the max element from the array +int getMax(int a[], int size) +{ + int max = a[1]; + for (int i = 2; i <= size; i++) + { + if (a[i] > max) + max = a[i]; + } + return max; +} +void countSort(int *a, int size); + +int main() +{ + cout << "Enter the length of array" << endl; + int n; + cin >> n; + int *a = new int(n + 1); + // Getting elements of array + cout << "Enter the elements of array" << endl; + for (int i = 1; i <= n; i++) + cin >> a[i]; + cout << "Original array: \n"; + for (int i = 1; i <= n; i++) + cout << a[i] << " "; + countSort(a, n); + cout << "\nSorted array: \n"; + for (int i = 1; i <= n; i++) + cout << a[i] << " "; + delete (a); + return 0; +} + +void countSort(int *a, int size) +{ + int output[size + 1]; + int max = getMax(a, size); + int count[max + 1]; //create count array (max+1 number of elements) + for (int i = 0; i <= max; i++) + count[i] = 0; //initialize count array to all zero + for (int i = 1; i <= size; i++) + count[a[i]]++; //increase number count in count array. + for (int i = 1; i <= max; i++) + count[i] += count[i - 1]; //find cumulative frequency + for (int i = size; i >= 1; i--) + { + output[count[a[i]]] = a[i]; + count[a[i]] -= 1; //decrease count for same numbers + } + for (int i = 1; i <= size; i++) + a[i] = output[i]; //store output array to main array +} \ No newline at end of file