Advertisement
Tariqul_Islam

Insertion sort

Jul 24th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int n,i,j;
  5.     scanf("%d",&n);
  6.     int a[n];
  7.     for(j=0; j<n; j++)
  8.     {
  9.         scanf("%d",&a[j]);
  10.     }
  11.  
  12.     for(j=1; j<n; j++)
  13.     {
  14.         int key=a[j];
  15.         i=j-1;
  16.         while(i>=0 && a[i]>key)
  17.         {
  18.             a[i+1]=a[i];
  19.             a[i]=key;
  20.             i=i-1;
  21.  
  22.         }
  23.  
  24.  
  25.     }
  26.     for(i=0; i<n; i++)
  27.     {
  28.         printf("%d ",a[i]);
  29.     }
  30.     printf("\n");
  31.  
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement