upsidedown

INSERTION SORT WITH O/P

Oct 4th, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void Insertsort(int data[],int n)
  5. {
  6.     int temp,i,j;
  7.     printf("Initial array is\n");
  8.     for(i=0;i<n;i++)
  9.     {
  10.         printf("%d\t",data[i]);
  11.     }
  12.  
  13.     for(i=0;i<n-1;i++)
  14.     {
  15.         for(j=1;j<n;j++)
  16.         {
  17.              temp=data[j];
  18.              i=j-1;
  19.              while(i>=0 && data[i]>temp)
  20.              {
  21.                  data[i+1]=data[i];
  22.                  i=i-1;
  23.                  data[i+1]=temp;
  24.              }
  25.         }
  26.  
  27.     }
  28.    
  29.     printf("\nThe final array is\n ");
  30.     for(i=0;i<n;i++)
  31.     {
  32.         printf("%d\t",data[i]);
  33.     }
  34. }
  35.  
  36. void main()
  37. {
  38.     int a[20],n,i,j;
  39.     printf("Enter the no of elemnets\n");
  40.     scanf("%d",&n);
  41.     printf("Enter elements\n");
  42.     for(i=0;i<n;i++)
  43.         scanf("%d",&a[i]);
  44.     Insertsort(a,n);
  45.     return 0;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. OUTPUT
  55.  
  56. Enter the no of elemnets
  57. 5
  58. Enter elements
  59. 4
  60. 5
  61. 9
  62. 1
  63. 2
  64. Initial array is
  65. 4       5       9       1       2
  66. The final array is
  67.  1      2       4       5       9       Press any key to continue
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment