C Programming Tutorial

 
 
 
 
#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
*****************************************************************************/

 
 
Didn't find what you were looking for? Find more on Program of insertion sort