diff --git a/CountingSort/C/counting_sort.c b/CountingSort/C/counting_sort.c new file mode 100644 index 000000000..37d084bef --- /dev/null +++ b/CountingSort/C/counting_sort.c @@ -0,0 +1,50 @@ +#include +#include +#define RANGE 255 + +// The main function that sort the given string arr[] in +// alphabatical order +void countSort(char arr[]) +{ + // The output character array that will have sorted arr + char output[strlen(arr)]; + + // Create a count array to store count of inidividul + // characters and initialize count array as 0 + int count[RANGE + 1], i; + memset(count, 0, sizeof(count)); + + // Store count of each character + for(i = 0; arr[i]; ++i) + ++count[arr[i]]; + + // Change count[i] so that count[i] now contains actual + // position of this character in output array + for (i = 1; i <= RANGE; ++i) + count[i] += count[i-1]; + + // Build the output character array + for (i = 0; arr[i]; ++i) + { + output[count[arr[i]]-1] = arr[i]; + --count[arr[i]]; + } + + // Copy the output array to arr, so that arr now + // contains sorted characters + for (i = 0; arr[i]; ++i) + arr[i] = output[i]; +} + +// Driver program to test above function +int main() +{ + char arr[1000] ; + printf("Enter a string : "); + scanf("%s",arr); + + countSort(arr); + + printf("Sorted character array is %s", arr); + return 0; +}