In this tutorial you will learn about the Comb Sort Algorithm and its application with practical example.
Comb Sort Program In C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <stdlib.h> int newgap(int gap) { gap = (gap * 10) / 13; if (gap == 9 || gap == 10) gap = 11; if (gap < 1) gap = 1; return gap; } void comb_sort(int a[], int aSize) { int gap = aSize; int temp, i; for (;;) { gap = newgap(gap); int swapped = 0; for (i = 0; i < aSize - gap; i++) { int j = i + gap; if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; swapped = 1; } } if (gap == 1 && !swapped) break; } } int main () { int n, i; int *a; printf("Enter number of elements: "); scanf("%d", &n); a = (int *)calloc(n, sizeof(int)); printf("Enter %d integer numbers\n", n); for(i = 0; i < n; i++) { scanf("%d",&a[i]); } comb_sort(a, n); printf("Elements after sorting: "); for(i = 0;i < n;i++) { printf("%d ", (a[i])); } return 0; } |
Output:-
Table Of Contents−