主题:[讨论]希尔排序的问题
下面是一个希尔排序的程序,为什么用a[10] = {23,4,145,3,44,11,42,56,73,35}这个系列就排不好???
#include <stdio.h>
void ShellSort(int a[], int n)
{
int i,flag=1,gap=n;
int temp;
while(gap>1)
{
while(flag==1)
{
flag = 0;
gap = gap/2;
for(i=0; i<n-gap; i++)
{
if(a[i]>a[i+gap])
{
temp = a[i];
a[i] = a[i+gap];
a[i+gap] = temp;
flag = 1;
}
}
//gap = gap/2;
//flag = 0;
}
}
}
void main()
{
int i;
//int a[10] = {23,4,145,3,44,11,42,56,73,35}; //这个是排不好
//ShellSort(a,10);
int a[10] = {3,6,4,12,11,10,8,9};
ShellSort(a,8);
for(i=0; i<8; i++)
{
printf("%d ",a[i]);
}
printf("\n");
}