Advertisement
SergeyPGUTI

6.3.9

Nov 21st, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void InsertionSort(int a[],int n)
  6. {
  7.     int temp;
  8.     for (int i=1;i<n;i++)
  9.     {
  10.         if (a[i]<a[i-1])
  11.             for (int j=i;j-1>=0 && a[j]<a[j-1];j--)
  12.         {
  13.             temp=a[j];
  14.             a[j]=a[j-1];
  15.             a[j-1]=temp;
  16.         }
  17.     }
  18. }
  19.  
  20. int main()
  21. {
  22.     int n;
  23.     cin>>n;
  24.     int * A=new int [n];
  25.     for (int i=0;i<n;i++)
  26.     {
  27.         cin>>A[i];
  28.     }
  29.     InsertionSort(A,n);
  30.  
  31.     for (int i=0;i<n;i++)
  32.     {
  33.         cout<<A[i]<<" ";
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement