This C program sorts elements of an integer array using Counting sort. This is not very space efficient algorithm.

Here is the source code of the C program to display sorted list using Counting sort. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program for counting sort`

`*/`

`#include <stdio.h>`

`/* Counting sort function */`

void counting_sort(int A[], int k, int n)

`{`

int i, j;

int B[15], C[100];

for (i = 0; i <= k; i++)

C[i] = 0;

for (j = 1; j <= n; j++)

C[A[j]] = C[A[j]] + 1;

for (i = 1; i <= k; i++)

C[i] = C[i] + C[i-1];

for (j = n; j >= 1; j--)

`{`

B[C[A[j]]] = A[j];

C[A[j]] = C[A[j]] - 1;

`}`

printf("The Sorted array is : ");

for (i = 1; i <= n; i++)

printf("%d ", B[i]);

`}`

`/* End of counting_sort() */`

`/* The main() begins */`

int main()

`{`

int n, k = 0, A[15], i;

printf("Enter the number of input : ");

scanf("%d", &n);

printf("\nEnter the elements to be sorted :\n");

for (i = 1; i <= n; i++)

`{`

scanf("%d", &A[i]);

if (A[i] > k) {

k = A[i];

`}`

`}`

counting_sort(A, k, n);

printf("\n");

return 0;

`}`

$ gcc counting_sort.c $ a.out Enter the number of input : 10 Enter the elements to be sorted : 21 4 18 15 24 6 9 1 5 4 The Sorted array is : 1 4 4 5 6 9 15 18 21 24

**Sanfoundry Global Education & Learning Series – 1000 C Algorithms.**

If you wish to look at all C Algorithms and Solutions, go to C Algorithms.