排序方法3---希尔排序

#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;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Yun_Ge/article/details/85476710