#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("希尔排序示例: "); printf("Please input n above 1 and below %d ",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: "); for(i=1;i<=n;i++) { scanf("%d",&R[i]); } printf("The array you input is : "); for(i=1;i<=n;i++) { printf("%d ",R[i]); } Shell_Sort(n); printf("The array after Shell order is: "); for(i=1;i<=n;i++) { printf("%d ",R[i]); } return 0; }