C Programming Code Examples C > Sorting Searching Code Examples Program to Perform Comb Sort on Array of Integers Program to Perform Comb Sort on Array of Integers 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. #include <stdio.h> #include <stdlib.h> /*Function to find the new gap between the elements*/ int newgap(int gap) { gap = (gap * 10) / 13; if (gap == 9 || gap == 10) gap = 11; if (gap < 1) gap = 1; return gap; } /*Function to implement the combsort*/ void combsort(int a[], int aSize) { int gap = aSize; int temp, x; for (;;) { gap = newgap(gap); int swapped = 0; for (x = 0; x < aSize - gap; x++) { int j = x + gap; if (a[x] > a[j]) { temp = a[x]; a[x] = a[j]; a[j] = temp; swapped = 1; } } if (gap == 1 && !swapped) break; } } int main () { int n, x; int *a; printf("Please insert the number of elements to be sorted: "); scanf("%d", &n); // The total number of elements a = (int *)calloc(n, sizeof(int)); for (x = 0;x< n;x++) { printf("Input element %d :", x); scanf("%d", &a[x]); // Adding the elements to the array } printf("unsorted list"); // Displaying the unsorted array for(x = 0;x < n;x++) { printf("%d", a[x]); } combsort(a, n); printf("Sorted list:\n"); // Display the sorted array for(x = 0;x < n;x++) { printf("%d ", (a[x])); } return 0; }