From 4e6acdc4ed3c675434d9378ee26194d173eda592 Mon Sep 17 00:00:00 2001 From: Paawan Mukker Date: Wed, 10 Oct 2018 00:13:01 +0530 Subject: [PATCH] counting_sort.c: Add Counting Sort Algorithm This adds Counting sort which sorts array by counting the occurences of elements. Closes https://github.com/NITSkmOS/Algorithms/issues/381 --- README.md | 2 +- counting_sort/C/counting_sort.c | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 counting_sort/C/counting_sort.c diff --git a/README.md b/README.md index a9b8f78a..c617a410 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This repository contains examples of various algorithms written on different pro | [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) | | [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | | | | [:octocat:](merge_sort/Python) | | [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) | -| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) | +| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | [:octocat:](counting_sort/C) | | | [:octocat:](counting_sort/Python) | | [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) | | [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) | | [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | | diff --git a/counting_sort/C/counting_sort.c b/counting_sort/C/counting_sort.c new file mode 100644 index 00000000..db9ee993 --- /dev/null +++ b/counting_sort/C/counting_sort.c @@ -0,0 +1,73 @@ +#include +#include + +/* +Counting sort implementation for sorting given array. +Note - Works on Non-negative integer array only. + +Parameters: +int a[] - Integer array a to be sorted. +int n - size of array a. +int r. - Range of numbers in a. +*/ + +void counting_sort(int a[], int n, int r) { + if(a == NULL) return; + + //array to store count of each occurences + int *cnt = (int *) malloc((r+1)*sizeof(int)); // Ignore CPPLintBear + //Out array to store sorted values + int *out = (int *) malloc((n)*sizeof(int)); // Ignore CPPLintBear + + + // Intitialise the count array to 0 + for (int i = 0; i <= r; ++i) + cnt[i] = 0; + + // Count each occurences digit wise + for (int i = 0; i < n; ++i) + cnt[a[i]]++; + + // Cumulative count array + for (int i = 1; i <= r; ++i) + cnt[i]+=cnt[i-1]; + + // Sort + for (int i = n - 1; i >= 0; i--) { + // Note - starting from n-1 to maintain "stable sort" property + out[cnt[a[i]] - 1] = a[i]; + cnt[a[i]]--; + } + + // Copy results back to orignal array + for (int i = 0; i < n; i++) + a[i] = out[i]; +} + + +int main() { + int a[] = {1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1 }; + int n = sizeof(a)/sizeof(a[0]); + + // Get the max element for calculating range of number + int max_a = a[0]; + for (int i = 1; i < n; ++i) + if(max_a < a[i]) max_a = a[i]; + + + printf("Input Array:\n"); + for (int i = 0; i < n; i++) { + printf("%d ", a[i]); + } + printf("\n"); + + counting_sort(a, n, max_a); + + printf("Sorted Array:\n"); + for (int i = 0; i < n; i++) { + printf("%d ", a[i]); + } + printf("\n"); + + return 0; +}