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