// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
int min=arr[i];
int j=i-1;
while(j>=0 && arr[j]>min){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=min;
// cout<<"iretation "<<i+1<<":";
// for(int k=0;k<n;k++){
// cout<<arr[k]<<" ";
// }
// cout<<endl;
}
cout<<"after sorted using insertion sort:"<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter