#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 10
void shellSort(int a[], int n)
{
int step, i, j, tmp = 0 ;
for (step = n / 2; step > 0; step /= 2)
{
for (i = step; i < n; i++)
{
for (j = i - step; j >= 0 && a[j] > a[j + step]; j -= step)
{
tmp = a[j];
a[j] = a[j + step];
a[j + step] = tmp;
}
}
}
}
void printA(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%3d", a[i]);
}
printf("\n");
}
int main()
{
int a[NUM];
srand(time(0));
for (int i = 0; i < NUM; i++)
{
a[i] = rand() % 99 + 1;
}
printf("原始数据:\n");
printA(a, NUM);
shellSort(a, NUM);
printf("希尔排序后的数据:\n");
printA(a, NUM);
system("pause");
return 0;
#include<stdlib.h>
#include<time.h>
#define NUM 10
void shellSort(int a[], int n)
{
int step, i, j, tmp = 0 ;
for (step = n / 2; step > 0; step /= 2)
{
for (i = step; i < n; i++)
{
for (j = i - step; j >= 0 && a[j] > a[j + step]; j -= step)
{
tmp = a[j];
a[j] = a[j + step];
a[j + step] = tmp;
}
}
}
}
void printA(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%3d", a[i]);
}
printf("\n");
}
int main()
{
int a[NUM];
srand(time(0));
for (int i = 0; i < NUM; i++)
{
a[i] = rand() % 99 + 1;
}
printf("原始数据:\n");
printA(a, NUM);
shellSort(a, NUM);
printf("希尔排序后的数据:\n");
printA(a, NUM);
system("pause");
return 0;
}
运行结果: