C program for Comb Sort Algorithm | C code champ

# C program for Comb Sort Algorithm | C code champ

3510
3
SHARE

C program for Comb Sort Algorithm : Combo sort is an sorting algorithm. It is an efficient variation of bubble sort that eliminates “turtles”, the small values near the end of the list and makes use of gaps between values.

How the Combo sort algorithm works :

In comb sort, using Gaps properly is the main part . For example, in bubble sort the gap between two elements was 1 while here the gap starts out as a large value and shrinks until it reaches the value 1, when it practically becomes bubble sort. The shrink factor determines how much the gap is lessened. The value is crucial, so an ideal value would be 13.

So how to achieve the same using C logic. Lets first see how our code going to work. At first, we will calculate the size of the gap, based on the number of elements to be sorted. The Comb sort method is the one who sorts the from the array, at first with the highest gap, which is calculated by calling the Gap logic, then it will pass through the array and checks the proper elements if they are in the right order, and if not, then we will swap the values. This continues until the gap reaches the value 1 and no more swaps have been executed. Now lets see .

### C program of Comb Sort Algorithm :

```#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int newGap(int gap) {
gap = (gap * 10) / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1)
gap = 1;
return gap;
}
void combsort(int a[], int aSize) {
int gap = aSize;
for (;;) {
gap = newGap(gap);
bool swapped = false;
for (int i = 0; i < aSize - gap; i++) {
int j = i + gap;
if (a[i] > a[j]) {
int temp=a[i];
a[i]=a[i + gap];
a[i + gap]=temp;
swapped = true;
}
}
if (gap == 1 && !swapped)
break;
}
}
int main ()
{
int n;
int *a;
printf("-----------------------------------------------------------\n");
printf("----------------------Made by C code champ-----------------\n");
printf("-----------------------------------------------------------\n\n");
printf("\t\t\tCOMB SORT\n\n\n\n");
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(int i=0;i< n;i++)
{

printf("Input %d element: ",i);
scanf("%d",&a[i]); // Adding the elements to the array
}
printf("\n\n\nUnsorted list:\n");       // Displaying the unsorted array
for(int i=0;i< n;i++)
{
printf("%d\t",a[i]);
}
combsort(a,n);
printf("\n\n\nSorted list:\n");        // Display the sorted array
for(int i=0;i < n;i++)
{
printf("%d\t",a[i]);
}
getch();
return 0;
}```

We hope you all have enjoyed the . If you have any issues with the code let us know in form of comments.

1. Good way of telling, and pleasant article to obtain information concerning my presentation focus, which i am going to deliver in academy.

2. I do consider all of the ideas you have introduced to your post. They’re very convincing and will definitely work. Nonetheless, the posts are very short for novices. Could you please lengthen them a little from subsequent time? Thank you for the post.

3. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is fantastic, let alone the content!. Thanks For Your article about C program for Comb Sort Algorithm .