#include<iostream>
using namespace std;
void insert_sort(int a[], int n);
int main(){
int a[]={ 3,7,2,9,6,5,8,1,4};
insert_sort(a,9);
return 0;
}
void insert_sort(int a[], int n)
{
int i, j, temp,step;
int temp2;
for(step=n/2;step>=1;step=step/2)
{
for (i = step; i < n; ++i)
{
temp = a[i];
for(j=i-step;j>=0&&temp<a[j];j-=step){
temp2= a[j+step]=a[j];
}
a[j+step]=temp;
}
}//for
for (i = 0; i < n; ++i){
cout<<a[i]<<" "<<endl;
}
}//insert
上面是正常版本
下面是有问题版本
错误的原因是for循环的终止条件设置有问题,j应该是j>=step 按照这个例子