C Program to Implement Counting Sort

This C program sorts a given array of integer numbers using Counting Sort technique.

Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items.

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

  1. /*
  2.  * C Program for counting sort
  3.  */
  4. #include <stdio.h>  
  5.  
  6. /*  Counting sort function  */
  7. void counting_sort(int A[], int k, int n)
  8. {
  9.     int i, j;
  10.     int B[15], C[100];
  11.     for (i = 0; i <= k; i++)
  12.         C[i] = 0;
  13.     for (j = 1; j <= n; j++)
  14.         C[A[j]] = C[A[j]] + 1;
  15.     for (i = 1; i <= k; i++)
  16.         C[i] = C[i] + C[i-1];
  17.     for (j = n; j >= 1; j--)
  18.     {
  19.         B[C[A[j]]] = A[j];
  20.         C[A[j]] = C[A[j]] - 1;
  21.     }
  22.     printf("The Sorted array is : ");
  23.     for (i = 1; i <= n; i++)
  24.         printf("%d ", B[i]);
  25. }
  26. /*  End of counting_sort()  */
  27.  
  28. /*  The main() begins  */
  29. int main()
  30. {
  31.     int n, k = 0, A[15], i;
  32.     printf("Enter the number of input : ");
  33.     scanf("%d", &n);
  34.     printf("\nEnter the elements to be sorted :\n");
  35.     for (i = 1; i <= n; i++)
  36.     {
  37.         scanf("%d", &A[i]);
  38.         if (A[i] > k) {
  39.             k = A[i];
  40.         }
  41.     }
  42.     counting_sort(A, k, n);
  43.     printf("\n");
  44.     return 0;
  45. }

$ gcc countsort.c -o countsort
$ ./countsort
 
Enter the number of elements : 10
 
Enter the elements to be sorted : 8 11 34 2 1 5 4 9 6 47
 
The Sorted array is :
1 2 4 5 6 8 9 11 34 47

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.