Advertisement
borsha06

insertion.cpp

Feb 27th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     int n,i,j,k,a[1000],key;
  7.     cin>>n;
  8.     for(j=1;j<=n;j++)
  9.         {
  10.             cin>>a[j];
  11.         }
  12.     for(j=2;j<=n;j++)
  13.     {
  14.         key=a[j];
  15.         i=j-1;
  16.         while(i>0 && a[i]>key)
  17.         {
  18.             a[i+1]=a[i];
  19.             i=i-1;
  20.  
  21.         }
  22.         a[i+1]=key;
  23.  
  24.     }
  25.     for(j=1;j<=n;j++)
  26.     {
  27.         cout<<a[j]<<endl;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement