一、直接插入排序
最好情况:O(n)
最坏情况:O(n^2)
稳 定 性 :稳定
// 5 8 9 6 4 10
void InsertSort(int a[],int n){ //已6开始为例
for(int i = 1; i < n ; ++i)
{
if(a[i]<a[i-1])
{
int temp = a[i];//保存a[i]
for(int j = i-1 ; j>=0 && a[j]>temp ; --j)
{
a[j+1]=a[j];//把比temp大的后移
}
a[j+1]=temp;//移完以后,j在待插入位置的前一位
}
}
}