Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program of insertion sort

Posted By: Abelerd Fischer     Category: C Programming     Views: 10156

Write a program of insertion sort.

Code for Program of insertion sort in C Programming

#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{

    int k[size],i,p,temp;
    clrscr();
    printf("\nENTER NO\n");
    for(i=1;i<=size;i++)
    {
        scanf("%d",&k[i]);
    }
    for(i=2;i<=size;i++)
    {
        p = i;
        for(;(k[p]<k[p-1]) && p>1;p--)
        {
            temp = k[p];
            k[p] = k[p-1];
            k[p-1] = temp;
        }
        printf("\npass %d : ",i);
        for(p=1;p<=size;p++)
        {
            printf("%d\t",k[p]);
        }
    }
    printf("\n\nSORTED ARRAY.\n");
    for(p=1;p<=size;p++)
    {
        printf("%d\t",k[p]);
    }

getch();
}


/*****************************************************************************    

ENTER NO
1
7
2
5
3

pass 2 : 1 7 2 5 3
pass 3 : 1 2 7 5 3
pass 4 : 1 2 5 7 3
pass 5 : 1 2 3 5 7

SORTED ARRAY.
1 2 3 5 7
*****************************************************************************/
  
Share: 


Didn't find what you were looking for? Find more on Program of insertion sort Or get search suggestion and latest updates.

Abelerd Fischer
Abelerd Fischer author of Program of insertion sort is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Hasib Chowdhury from United States Comment on: Jan 27
Fine program , Thanks

View All Comments