Rabu, 18 Desember 2013

Insertion Sort C Program Insertion Function Sorting Array



C Program Insertion Function Sorting



  1. //Analysis of Algorithms  
  2. //Insertion Sorting - C Program  
  3. //Sort Array Using Insertion Function.  
  4. //Program by:- empu eko r
  5. //TESTED:- OK  
  6.   
  7. #include<stdio.h>  
  8. #include<conio.h>  
  9.   
  10.    void insertion(int a[],int n)  
  11.    {  
  12.   
  13.     int i,j,x,k;  
  14.   
  15.            for(i=1;i<=n-1;i++)  
  16.     
  17.                 {  
  18.                        j=i;  
  19.   
  20.                        x=a[i];  
  21.   
  22.   
  23.                        while(a[j-1]>x && j>0)  
  24.   
  25.                                  {  
  26.   
  27.                                    a[j]=a[j-1];  
  28.                                     j=j-1;  
  29.   
  30.                                  }  
  31.   
  32.                         a[j]=x;  
  33.   
  34.   
  35.                        printf("\n\n The array after pass no.%d: ",i);  
  36.                        for(k=0;k<=n-1;k++)  
  37.                       printf("%4d",a[k]);  
  38.   
  39.   
  40.                  }//end for.  
  41.   
  42.   
  43.    } //end function.  
  44.   
  45.    void main()  
  46.   
  47.    {  
  48.   
  49.      int a[1000],n,i;  
  50.   
  51.      clrscr();  
  52.   
  53.      printf("\n\nEnter an integer value for total no.s of elements to be sorted: ");  
  54.      scanf("%3d",&n);  
  55.   
  56.      for(i=0;i<=n-1;i++)  
  57.   
  58.           {  
  59.   
  60.             printf("\n\nEnter an integer value for element no.%d: ",i+1);  
  61.             scanf("%4d",&a[i]);  
  62.   
  63.           }  
  64.   
  65.   
  66.      insertion(a,n);  
  67.   
  68.   
  69.      printf("\n\n\nFinally sorted array is : ");  
  70.      for(i=0;i<=n-1;i++)  
  71.      printf("%4d",a[i]);  
  72.   
  73.   
  74.    }//end program.  
  75.   
  76. /* 
  77.  
  78. ----------sample output------------------ 
  79.  
  80.  
  81.  
  82. Enter an integer value for total no.s of elements to be sorted: 6 
  83.  
  84.  
  85. Enter an integer value for element no.1: 67 
  86.  
  87.  
  88. Enter an integer value for element no.2: 34 
  89.  
  90.  
  91. Enter an integer value for element no.3: -23 
  92.  
  93.  
  94. Enter an integer value for element no.4: 100 
  95.  
  96.  
  97. Enter an integer value for element no.5: 0 
  98.  
  99.  
  100. Enter an integer value for element no.6: -68 
  101.  
  102.  
  103. The array after pass no.1: 34 67 -23 100 0 -68 
  104.  
  105. The array after pass no.2: -23 34 67 100 0 -68 
  106.  
  107. The array after pass no.3: -23 34 67 100 0 -68 
  108.  
  109. The array after pass no.4: -23 0 34 67 100 -68 
  110.  
  111. The array after pass no.5: -68 -23 0 34 67 100 
  112.  
  113.  
  114. Finally sorted array is : -68 -23 0 34 67 100 
  115.  
  116.  
  117.  
  118. */ 
  1. //Analysis of Algorithms  
  2. //Insertion Sorting - C Program  
  3. //Sort Array Using Insertion Function.  
  4. //Program by:- GAURAV AKRANI.   
  5. //TESTED:- OK  
  6.   
  7. #include<stdio.h>  
  8. #include<conio.h>  
  9.   
  10.    void insertion(int a[],int n)  
  11.    {  
  12.   
  13.     int i,j,x,k;  
  14.   
  15.            for(i=1;i<=n-1;i++)  
  16.     
  17.                 {  
  18.                        j=i;  
  19.   
  20.                        x=a[i];  
  21.   
  22.   
  23.                        while(a[j-1]>x && j>0)  
  24.   
  25.                                  {  
  26.   
  27.                                    a[j]=a[j-1];  
  28.                                     j=j-1;  
  29.   
  30.                                  }  
  31.   
  32.                         a[j]=x;  
  33.   
  34.   
  35.                        printf("\n\n The array after pass no.%d: ",i);  
  36.                        for(k=0;k<=n-1;k++)  
  37.                       printf("%4d",a[k]);  
  38.   
  39.   
  40.                  }//end for.  
  41.   
  42.   
  43.    } //end function.  
  44.   
  45.    void main()  
  46.   
  47.    {  
  48.   
  49.      int a[1000],n,i;  
  50.   
  51.      clrscr();  
  52.   
  53.      printf("\n\nEnter an integer value for total no.s of elements to be sorted: ");  
  54.      scanf("%3d",&n);  
  55.   
  56.      for(i=0;i<=n-1;i++)  
  57.   
  58.           {  
  59.   
  60.             printf("\n\nEnter an integer value for element no.%d: ",i+1);  
  61.             scanf("%4d",&a[i]);  
  62.   
  63.           }  
  64.   
  65.   
  66.      insertion(a,n);  
  67.   
  68.   
  69.      printf("\n\n\nFinally sorted array is : ");  
  70.      for(i=0;i<=n-1;i++)  
  71.      printf("%4d",a[i]);  
  72.   
  73.   
  74.    }//end program.  
  75.   
  76. /* 
  77.  
  78. ----------sample output------------------ 
  79.  
  80.  
  81.  
  82. Enter an integer value for total no.s of elements to be sorted: 6 
  83.  
  84.  
  85. Enter an integer value for element no.1: 67 
  86.  
  87.  
  88. Enter an integer value for element no.2: 34 
  89.  
  90.  
  91. Enter an integer value for element no.3: -23 
  92.  
  93.  
  94. Enter an integer value for element no.4: 100 
  95.  
  96.  
  97. Enter an integer value for element no.5: 0 
  98.  
  99.  
  100. Enter an integer value for element no.6: -68 
  101.  
  102.  
  103. The array after pass no.1: 34 67 -23 100 0 -68 
  104.  
  105. The array after pass no.2: -23 34 67 100 0 -68 
  106.  
  107. The array after pass no.3: -23 34 67 100 0 -68 
  108.  
  109. The array after pass no.4: -23 0 34 67 100 -68 
  110.  
  111. The array after pass no.5: -68 -23 0 34 67 100 
  112.  
  113.  
  114. Finally sorted array is : -68 -23 0 34 67 100 
  115.  
  116.  
  117.  
  118. */ 

0 komentar:

Posting Komentar