Rakibul_Ahasan

Insertion_Sort

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