C Program to Sort an Array using Gnome Sort

By | 27.11.2016

Sort an Array using Gnome Sort


Write a C Program to Sort an Array using Gnome Sort. Here’s simple C Program to Sort an Array using Gnome Sort in C Programming Language.


Gnome sort(stupid sort) is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in bubble sort. It is conceptually simple, requiring no nested loops.


Below is the source code for C Program to Sort an Array using Gnome Sort which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Sort an Array using GnomeSort  */

#include <stdio.h>
 
void main()
{
    int i, temp, ar[10], n;
 
    printf("\nenter the elemts number u would like to enter:");
    scanf("%d", &n);
    printf("\nenter the elements to be sorted through gnome sort:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &ar[i]);
    i = 0;
    while (i < n)
    {
        if (i == 0 || ar[i - 1] <= ar[i])
            i++;
        else
        {
            temp = ar[i-1];
            ar[i - 1] = ar[i];
            ar[i] = temp;
            i = i - 1;
        }
    }
    for (i = 0;i < n;i++)
        printf("%d\t", ar[i]);
}

Output :


/*  C Program to Sort an Array using GnomeSort  */

enter the elemts number u would like to enter:5

enter the elements to be sorted through gnomesort:
9
5
2
4
3
2       3       4       5       9

Above is the source code for C Program to Sort an Array using Gnome Sort which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Category: C Programming Sorting Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments