Insertion Sort

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:         int a[25], n, i;  
6:         void sort(int a[25], int n);  
7:         clrscr();  
8:         printf("Enter the number of elements : ");  
9:              scanf("%d", &n);  
10:         printf("\nEnter the elements : \n");  
11:         for(i=0;i<n;i++)  
12:                scanf("%d", &a[i]);  
13:         sort(a,n);  
14:         getch();  
15:  }  
16:  void sort(int a[25], int n)  
17:  {  
18:         int temp, i, j, k;  
19:         clrscr();  
20:  for(i=1;i<n;i++)  
21:         {       
22:                temp=a[i];  
23:                for(j=i; j>0; j--)  
24:                {  
25:                   if(temp<a[j-1])  
26:                   {  
27:                          a[j]=a[j-1];  
28:                          continue;  
29:                   }  
30:                   else  
31:                     goto g;  
32:                }  
33:                g:  
34:             a[j]=temp;  
35:            if(i<n-1)  
36:            {  
37:                      printf("\n\n\n\t\t\tSorted array after scan %d \n\n\t\t", i);  
38:                     for(k=0;k<n;k++)  
39:                        printf("%d\t",a[k]);  
40:            }  
41:         }  
42:         printf("\n\n\n\t\t\tSORTED ARRAY\n\n\t\t");  
43:         for(i=0;i<n;i++)  
44:                printf("%d\t", a[i]);  
45:  }  

No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive