void display(int a[],int len)
{
for (int i=0;i<len;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
bool com(int a,int b)
{
return a>=b?1:0;
}
void StraightInsertSort(int a[],bool(*com)(int,int),int len)
{
for (int i=1;i<len;i++) //1-len-1 一次插入到前面的序列中
{
int temp=a[i];
int j=i-1;
if (com(a[i],a[j])) //如果是a[i] 大于等于a[i-1] 则不用交换
{
continue;
}
else
{
while (com(a[j],temp))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
display(a,8);
}
}
}
int main()
{
int a[8]={45,33,8,2,3,7,12,3};
StraightInsertSort(a,com,8);
display(a,8);
system("pause");
return 0;
}
直接插入排序
最新推荐文章于 2020-10-12 20:15:31 发布