From 810b0f005b8fbc0f29fb4bdf8ebfc21557b2d5b7 Mon Sep 17 00:00:00 2001 From: Vaibhav Singhal Date: Mon, 11 Oct 2021 21:13:44 +0530 Subject: [PATCH] Created Counting_Sort.c --- C/Algorithms/Sorting/Counting_Sort.c | 57 ++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 C/Algorithms/Sorting/Counting_Sort.c diff --git a/C/Algorithms/Sorting/Counting_Sort.c b/C/Algorithms/Sorting/Counting_Sort.c new file mode 100644 index 000000000..9067da5b3 --- /dev/null +++ b/C/Algorithms/Sorting/Counting_Sort.c @@ -0,0 +1,57 @@ +#include +int main() +{ + printf("Kindly enter the number of elements: "); + int n; + int i=0; + scanf("%d",&n); + + int arr[n]; + printf("Kindly enter the array elements: "); + for(i=0;i x) + x = arr[i]; + } + + + int count_arr[10]; + + for (int i = 0; i <= x; ++i) { + count_arr[i] = 0; + } + + for (int i = 0; i < n; i++) { + count_arr[arr[i]]++; + } + + for (int i = 1; i <= x; i++) { + count_arr[i] += count_arr[i - 1]; + } + + + for (int i = n - 1; i >= 0; i--) { + arr1[count_arr[arr[i]] - 1] = arr[i]; + count_arr[arr[i]]--; + } + + for (int i = 0; i < n; i++) { + arr[i] = arr1[i]; + } +} \ No newline at end of file