alaminrifat

Insertion Sort in C++

Nov 9th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl '\n'
  4.  
  5.  
  6. int main(){
  7.     int n = 10 , arr[n]={6,2,12,23,11,56,85,41,203,101};
  8.     int temp,j;
  9.  
  10.     for (int i = 0; i < n ; i++ ){
  11.         j = i ;
  12.         while(j>0 && arr[j]<arr[j-1]){
  13.             temp = arr[j];
  14.             arr[j] = arr[j-1];
  15.             arr[j-1] = temp ;
  16.             j--;
  17.         }
  18.  
  19.     }
  20.     cout<<"After Sorting"<<nl;
  21.     for (int i = 0; i < n ; i++ ){
  22.         cout<<arr[i]<<" ";
  23.     }
  24.  
  25.  
  26.  
  27. }
  28.  
Add Comment
Please, Sign In to add comment