For each time, extract the next unsorted element from 1,2,…n,if current sorted element bigger than unsorted element, move sorted element to the right by 1, or insert the extracted element
/*
insertion sort
language:c++
author:zhoutonglx@qq.com
*/
#include<bits/stdc++.h>
using namespace std;
void InsertSort(int L[],int n){
int i,j;
int tmp;
for(i=1;i<n;i++){
tmp = L[i];
for(j=i;j;j--){
if(tmp>=L[j-1])
break;
L[j] = L[j-1];
}
L[j] = tmp;
}
}
int main(){
int L[] = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
int n = sizeof(L)/4;
InsertSort(L,n);
for(int i=0;i<n;i++){
cout<<L[i]<<' ';
}
cout<<endl;
}