题目内容
以下程序利用快速排序和选择排序算法实现输入数据的排序,请补充完整代码。#define MAXSIZE 50 //顺序表最大长度typedef int KeyType; //定义关键字类型为整型typedef struct{KeyType key; //关键字项}RedType; // 记录类型typedef struct{RedType r[MAXSIZE+1]; //r[0]闲置或用作哨兵单元int length; //顺序表长度}SqList; //顺序表类型void CreateList(SqList &L) /*输入原始数据,以-1结束 */{int data, k;printf("\n\n输入初始数据(每个数据以空格隔开,-1 结束) : \n\n");k = 0;scanf("%d",&data);while(data!= -1){k++;(1) ;scanf("%d",&data);}L.length=k;}void PrintList(SqList L, int n) /*输出原始数据*/{int i;printf("\n\n 排序前的数据 : ");for(i = 0; i < n; i++)printf(" %d",L.r[i+1].key); // 注意:r[0]用作哨兵单元printf("\n\n");}void PrintList1(SqList L, int n) /*输出排序后的数据*/{int i;printf("\n\n 排序后的数据 : ");for(i = 0; i < n; i++)printf(" %d",L.r[i+1].key);printf("\n\n");}int Partition(SqList &L, int low, int high){//一趟快速排序,返回i,产生了两个独立的待排子序列int i, j;KeyType pivotkey;L.r[0]= L.r[low];pivotkey=(2) ;while(low= pivotkey) && (low < high)) high--;(3) ;while((L.r[low].key <= pivotkey) && low < high) low++;(4) ;}L.r[low]=(5) ;return low;}void QuickSort(SqList &L, int low, int high) //快速排序{int pivotLoc;if(low< high){pivotLoc= Partition(L, low, high); //一趟快速排序QuickSort(L, low, (6) ); //对待排子序列进行快速排序QuickSort(L, (7) ,high);}}void SeleSort(SqList &L, int n) //直接选择排序{int i,j,k;RedType temp;for(i = 1; i < n; i++){k = i;for(j = i + 1; j <= n; j++)if(L.r[j].key < (8) )(9) ;if(k != i){temp = L.r[i];L.r[i] = L.r[k];L.r[k] = temp;}}}int main(){SqList L;CreateList(L);PrintList(L,L.length);printf("快速排序\n");QuickSort(L, 1,L.length);PrintList1(L,L.length);printf("直接选择排序\n");SeleSort(L, L.length);PrintList1(L,L.length);return 0;}
查看答案
搜索结果不匹配?点我反馈