#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 1000
int shell(int a[],int n)
{
int j, gap;
for (gap = n/2; gap > 0; gap /= 2)
for (j = gap; j < n; j++)
if (a[j] < a[j - gap])
{
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
}
int main()
{
int a[N],i;
memset(a,0,sizeof(a));
srand((unsigned int)time(NULL));
for(i=0;i<N;i++)
a[i]=rand()%100+1;
shell(a,N);
for(i=0;i<N;i++)
printf("%d ",a[i]);
#include<string.h>
#include<stdlib.h>
#define N 1000
int shell(int a[],int n)
{
int j, gap;
for (gap = n/2; gap > 0; gap /= 2)
for (j = gap; j < n; j++)
if (a[j] < a[j - gap])
{
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
}
int main()
{
int a[N],i;
memset(a,0,sizeof(a));
srand((unsigned int)time(NULL));
for(i=0;i<N;i++)
a[i]=rand()%100+1;
shell(a,N);
for(i=0;i<N;i++)
printf("%d ",a[i]);
}
运用了http://blog.csdn.net/morewindows/article/details/6668714/的希尔排序