数据结构练习2~希尔排序~
每日学点新东西~感觉希尔排序是对插入排序的一种改进~程序代码:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> #define MAX 255 int R[MAX]={0}; void ShellPass(int d,int n) { /*希尔排序中的一趟排序,d为当前增量*/ int i=0; int j=0; for (i=d+1;i<=n;++i) if (R[i]<R[i-d]) { R[0]=R[i]; j=i-d; /*R[0]只是暂存单元,不是哨兵*/ do { /*查找R[i]的插入位置*/ R[j+d]=R[j]; /*后移记录*/ j=j-d; }while (j>0&&R[0]<R[j]); R[j+d]=R[0]; /*插入R[i]到正确的位置上*/ } } void Shell_Sort(int n) { int increment=n; /*增量初值,不妨设n>0*/ do { increment=increment/3+1; /*求下一增量*/ ShellPass(increment,n); /*一趟增量为increment的Shell插入排序*/ }while (increment>1); } int main() { int i=0; int n=0; /*clrscr()*/ system("cls"); puts("Please input total element number of the sequence:"); scanf("%d",&n); if (n<1||n>MAX) { printf("n must more than 0 and less than %d.\n",MAX); exit(0); } puts("Please input the elements one by one:"); for (i=1;i<=n;++i) scanf("%d",&R[i]); puts("The sequence you input is:"); for (i=1;i<=n;++i) printf("%4d",R[i]); Shell_Sort(n); puts("\nThe sequence after shell_sort is:"); for (i=1;i<=n;++i) printf("%4d",R[i]); puts("\n"); return 0; }
[此贴子已经被作者于2017-2-16 14:03编辑过]