排序算法的实现6.docxVIP

  • 1
  • 0
  • 约1.01万字
  • 约 14页
  • 2022-07-27 发布于四川
  • 举报
河南工程学院《数据结构与算法》课程设计 成果报告 排序算法的实现 2014年12月29日 3程序清单 #includestdio. h#includetime. h #includestdlib. h #define MAXE 20000 typedef int KeyType; typedef struct/*记录类型*/{ KeyTypo key;/*关键字项*/ //InfoType data;/*其他数据项,类型为 InfoType*/( RecType; void InsertSort (RecType R[], int length);〃直接插入排序 void BlnsertSort (RecType R[], int low, int high, int length);〃折半插入排序 void Shel 1 Sort(RecType R[], int n);〃希尔排序 void bubble_sort(RecType R[], int length);〃起泡排序 int FindPos(RecType R[], int low, int high); void Quicksort(RecType R[], int low, int high); int SclectMinKcy(RecType R[], int i, int length); void SelectSort (RecType R[], int length);〃简单项选择择排序 void showSort (RecType R[]);〃快速排序 void showSort_F(RecType R口); int main(void)( int val; int i; RecType R[MAXE]; srand((unsigned)time(NULL)); for(i=0;i=10;i++) R [ i]. key=(rand()%100+1); printf(〃产生的随机数序列为:〃); for(i=0;i=10;i++) printf (飞3d”, R[i]. key); printf C\n);printf (“随机输入1到6数字:〃); scanf(%d”, val); switch(val){ case 1: InsertSort(R, 10); printf(〃随机数经直接插入排序后:〃); showSort(R); break;case 2:BlnsertSort(R, 1, 10,10); printf(随机数经折半插入排序后:〃); showSort(R);break; case 3:ShellSort(R, 10); printf (〃随机数经希尔插入排序后:〃); showSortF(R); break;case 4:bubble sort(R, 10); printf (〃随机数经起泡排序后:”); showSort_F(R); break;case 5:Quicksort. (R, 0, 9); printf(随机数经快速排序后:”); showSort_F(R);case 6:SelectSort(R, 10); printf(〃随机数经简单项选择择排序后:〃); showSort_F(R);}return 0;} void InsertSort(RecType R[], int length) { //对数组a作直接插入排序 int i, j; for(i=2;i=length;++i) if (R[i]. keyR[i-l]. key) // 需将 a[i]插入有序子表 { R[0]. key=R[i]. key; // 复制为哨兵R[i]. key=R[i-l]. key; for(j=i-2;R[0]. keyR[j]. key;-j)R[j+1]. key=R[j]. key; // 记录后移 R[j+1]. key=R[0]. key; // 插入到正确位置 } )void BlnsertSort (RecType R[], int low, int high, int length) ( int m; for ( int i=2; i=lcngth; ++i ) (R[0].key = R[i]. key; // 将 R[i]暂存到 R[0] low = 1;high = i-1; while ( low = high){〃在R[low.. high]中折半查找插入的位置 m = (low+high)/2;// 折半if (R[0]. key R[m]. key) high = m-1;//插入点在低半区 else low = m+1; //插入点在高半区 )//while for ( int j=i-

文档评论(0)

1亿VIP精品文档

相关文档