希尔排序

希尔排序属于插入排序的一种。

基本思想:先将整个数组分组,然后分别插入.


#include<stdio.h>
#define MAX 250 

int R[MAX];
//某一确定增量d的希尔排序
void Shell_Pass(int d,int n)
{
	int i,j;
	for(i=d+1;i<=n;i++)
	{
		if(R[i]<R[i-d])
		{
			R[0]=R[i];
			j=i-d;
			do{
				R[j+d]=R[j];
				j=j-d;
			}while(j>0&&R[0]<R[j]);
			R[j+d]=R[0];
		}
	}
	
 } 
 
 void Shell_Sort(int n)
 {
 	int d=n;
 	do{
 		d=d/3+1;
 		Shell_Pass(d,n);
	 }while(d>1);
 }
 
 int main()
 {
 	int n,i,d;
 	printf("希尔排序示例:\n");
 	printf("Please input n above 1 and below %d\n",MAX);
 	scanf("%d",&n);
 	if(n<1||n>MAX)
 	{
 		printf("The n you input is not right!BYE!");
 		return 0;
	 }
	 printf("Please input the array one by one:\n");
	 for(i=1;i<=n;i++)
	 {
	 	scanf("%d",&R[i]);
	 }
	 printf("The array you input is :\n");
	 for(i=1;i<=n;i++)
	 {
	 	printf("%d ",R[i]);
	 }
	 Shell_Sort(n);
	 printf("The array after Shell order is:\n");
	 for(i=1;i<=n;i++)
	 {
	 	printf("%d ",R[i]);
	 }
	 return 0;
 }


你可能感兴趣的:(希尔排序)