直接插入排序
2017-03-08 17:02
169 查看
直接插入排序算法的时间复杂度为o(n*n)
void InsertSort(int *a,int Length)
{
int i,j;
for(i =1;i<Length;i++)
{
if(a[i]<a[i-1])
{
int temp = a[i];
for(j = i-1;j>=0&&temp>a[j];j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
}
void InsertSort(int *a,int Length)
{
int i,j;
for(i =1;i<Length;i++)
{
if(a[i]<a[i-1])
{
int temp = a[i];
for(j = i-1;j>=0&&temp>a[j];j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
}