C program of Gnome Sort Algorithm | C code champ

# C program of Gnome Sort Algorithm | C code champ

2407
1
SHARE

C program of Gnome Sort Algorithm : Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place in Gnome sort is done by a series of swaps, as in Bubble Sort. It is very simple algorithm, requiring no nested loops. The Gnome Sort algorithm always finds the first place where two adjacent elements are in the wrong order, and swaps them. It takes advantage of the fact that performing a swap can introduce a new out-of-order adjacent pair only right before or after the two swapped elements. It does not assume that elements forward of the current position are sorted, so it only needs to check the position directly before the swapped elements. Let us see how to write a .

### C program of Gnome Sort Algorithm :

```#include<stdio.h>
#include<conio.h>
void gnome_sort(int *a, int n)
{
int i=1, j=2, t;
# define swap(i, j) { t = a[i]; a[i] = a[j]; a[j] = t; }
while(i < n) {
if (a[i - 1] > a[i]) {
swap(i - 1, i);
if (--i) continue;
}
i = j++;
}
# undef swap
}

int main()
{
int n,i,k = 0, A[15];
printf("-----------------------------------------------------------\n");
printf("-----------------------------------------------------------\n\n");
printf("\t\tGNOME SORT ALGORITHM\n\n\n\n");
printf("Enter the number of input : ");
scanf("%d",&n);
printf("\n\nEnter the elements to be sorted :\n");
for ( i = 0; i < n; i++)
{
scanf("%d",&A[i]);
}
gnome_sort(A,n);
printf("\n\nSorted List by Gnome Sort :\n");
for ( i = 0; i < n; i++)
{
printf("%d\t",A[i]);
}
getch();
}```

We hope that you all have enjoyed the of Gnome sort algorithm. If you have any doubts or queries ask us in form of comments.