rafid_shad

Insertion sort

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