#include<stdio.h>
#include<iostream>
#define MAXL 100
typedef int KeyType;
typedef char InfoType;
typedef struct
{
KeyType key;
InfoType data;
}RecType;
void Display(RecType R[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d", R[i].key);
}
printf("\n");
}
void ShellSort(RecType R[], KeyType keys[], int n)
{
int i, j, d;
RecType temp;
for (int i = 0; i < n; i++)
{
R[i].key = keys[i];
}
printf("排序前:");
Display(R, n);
for (d = n / 2; d>0; d = d / 2)
{
for (i = d; i < n; i++)
{
temp = R[i];
j = i - d;
while (j >= 0 && temp.key<R[j].key)
{
R[j + d] = R[j];
j = j - d; //和同组的排在前面的数据进行再次比较
}
R[j + d] = temp;
}
printf(" d = %d:", d);
Display(R, n);
}
}
int main()
{
int n = 10;
RecType R[MAXL];
KeyType a[] = { 3,7,4,2,8,1,9,0,5,6 };
ShellSort(R, a, n);
printf("排序后:");
Display(R, n);
system("pause");
return 1;
}
排序方法3---希尔排序
最新推荐文章于 2023-09-23 16:26:22 发布