SabirSazzad

Insertion Sort

Mar 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int i,l,x,temp;
  6.     cout<<"Enter Array Size: ";
  7.     cin>>l;
  8.     int *arr = new int[l+1];
  9.     cout<<"Enter Array Elements.... " <<endl;
  10.     for(i=0;i<l;i++)
  11.     {
  12.         cin>>arr[i];
  13.     }
  14.     for(i=1;i<=l-1;i++)
  15.     {
  16.         x=i;
  17.         while(x>0 && arr[x]<arr[x-1])
  18.         {
  19.             temp=arr[x];
  20.             arr[x]=arr[x-1];
  21.             arr[x-1]=temp;
  22.             x--;
  23.         }
  24.  
  25.     }
  26.     cout<<"Shorted list is..."<<endl;
  27.     for(i=0;i<=l-1;i++)
  28.     {
  29.         cout<<arr[i]<<" ";
  30.     }
  31.     return 0;
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment