Sunday, October 2, 2011

Insertion Sort C program for Dev c++


C Program Insertion Sorting 


 #include<stdio.h>  
 #include<conio.h>  
 #include<stdlib.h>  
 void insersort(int *,int );  
 int main()  
 {  
    int i,n,*a;  
    printf("\nEnter array size:");  
    scanf("%d",&n);  
    a=(int*)malloc(n*2);  
    printf("\nEnter array::\n");  
    for(i=1;i<=n;i++)  
    {  
            scanf("%d",a+i);printf("\n");  
    }  
    insersort(&a[0],n);  
    printf("\n\nArray in sorted order is-::\n");  
    for(i=1;i<=n;i++)  
    {  
            printf("%d",*(a+i));printf("\n");  
    }  
    getch();  
    return 0;  
 }  
 void insersort(int *b, int s)  
 {  
    int i,j,tmp;  
    for(i=1;i<=s;i++)  
    {  
             tmp=*(b+i);  
             for(j=i-1;j>0&&tmp<*(b+j);j--)  
             *(b+j+1)=*(b+j);   
               *(b+j+1)=tmp;  
    }  
 return;  
 }  

No comments:

Post a Comment