C Program to Implement Comb Sort Algorithm

This C Program performs Comb sort on array of integers. Comb sort is a comparison sorting algorithm. It is an exchange sort, similar to bubble sort.

Here is source code of the C Program to perform Comb sort on array of integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Perform Comb Sort on Array of Integers
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. /*Function to find the new gap between the elements*/
  8. int newgap(int gap)
  9. {
  10.     gap = (gap * 10) / 13;
  11.     if (gap == 9 || gap == 10)
  12.         gap = 11;
  13.     if (gap < 1)
  14.         gap = 1;
  15.     return gap;
  16. }
  17.  
  18. /*Function to implement the combsort*/
  19. void combsort(int a[], int aSize)
  20. {
  21.     int gap = aSize;
  22.     int temp, i;
  23.     for (;;)
  24.     {
  25.         gap = newgap(gap);
  26.         int swapped = 0;
  27.         for (i = 0; i < aSize - gap; i++) 
  28.         {
  29.             int j = i + gap;
  30.             if (a[i] > a[j])
  31.             {
  32.                 temp = a[i];
  33.                 a[i] = a[j];
  34.                 a[j] = temp;
  35.                 swapped  =  1;
  36.             }
  37.         }
  38.         if (gap  ==  1 && !swapped)
  39.             break;
  40.     }
  41. }
  42. int main ()
  43. {
  44.     int n, i;
  45.     int *a;
  46.     printf("Please insert the number of elements to be sorted: ");
  47.     scanf("%d", &n);       // The total number of elements
  48.     a  =  (int *)calloc(n, sizeof(int));
  49.     for (i = 0;i< n;i++)
  50.     {
  51.         printf("Input element %d :", i);
  52.         scanf("%d", &a[i]); // Adding the elements to the array
  53.     }
  54.     printf("unsorted list");       // Displaying the unsorted array
  55.     for(i = 0;i < n;i++)
  56.     {
  57.          printf("%d", a[i]);
  58.     }
  59.     combsort(a, n);
  60.     printf("Sorted list:\n");        // Display the sorted array
  61.     for(i = 0;i < n;i++)
  62.     {
  63.         printf("%d ", (a[i]));
  64.     }
  65.     return 0;
  66. }

$ cc combsort.c
$ a.out
Please insert the number of elements to be sorted: 10
Input element 0 :5
Input element 1 :6
Input element 2 :1
Input element 3 :3
Input element 4 :4
Input element 5 :7
Input element 6 :8
Input element 7 :9
Input element 8 :0
Input element 9 :6
unsorted list5613478906Sorted list:
0 1 3 4 5 6 6 7 8 9 
$ ./a.out
Please insert the number of elements to be sorted: 10
Input element 0 :1
Input element 1 :2
Input element 2 :3
Input element 3 :4
Input element 4 :5
Input element 5 :6
Input element 6 :7
Input element 7 :8
Input element 8 :9
Input element 9 :10
unsorted list12345678910Sorted list:
1 2 3 4 5 6 7 8 9 10 
$ ./a.out
Please insert the number of elements to be sorted: 10
Input element 0 :10
Input element 1 :9
Input element 2 :8
Input element 3 :7
Input element 4 :6
Input element 5 :5
Input element 6 :4
Input element 7 :3
Input element 8 :2
Input element 9 :1
unsorted list10987654321Sorted list:
1 2 3 4 5 6 7 8 9 10

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 wish to look at programming examples on all topics, go to C Programming Examples.

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.