void QuickSort(LineList r[], int first, int end) { /*快速排序算法函数*/ int i, j; LineList temp; i = first; j = end; temp = r[i]; /*初始化*/ while (i < j) { while (i < j && temp.Key <= r[j].Key) j--; r[i] = r[j]; while (i < j && r[i].Key <= temp.Key) i++; r[j] = r[i]; } r[i] = temp; // 添加输出语句,输出每次排序后的结果 printf("第 %d 次排序结果:", i-1); for(int k=first; k<=end; k++) { printf("%d ", r[k].Key); } printf("\n"); if (first < i - 1) QuickSort(r, first, i - 1); /*对左侧分区域进行快速排序*/ if (i + 1 < end) QuickSort(r, i + 1, end); /*对右侧分区域进行快速排序*/ } void SelectSort(LineList r[], int n) { /*直接选择排序算法*/ int i, j, k; /*k存放每趟比较的最大值的下标值*/ LineList x; for (i = 1; i <= n; i++) { k = i; for (j = i + 1; j <= n; ++j) if (r[j].Key < r[k].Key) /*选择本次比较的最大值*/ k = j; if (k != i) /*若k不等于当前位置i,则交换i和k位置的记录*/ { x = r[i]; r[i] = r[k]; r[k] = x; } } }这两个怎么样改人他显示出排序过程
时间: 2024-03-12 12:47:17 浏览: 13
您可以在每次排序后添加输出语句,输出排序结果。具体方法如下:在 QuickSort 和 SelectSort 函数中,在每次排序后添加以下代码:
```
// 添加输出语句,输出每次排序后的结果
printf("第 %d 次排序结果:", i-1);
for(int k=first; k<=end; k++)
{
printf("%d ", r[k].Key);
}
printf("\n");
```
这样就可以在每次排序后输出排序结果了。
相关问题
void QuickSort(LineList r[], int first, int end) { /*快速排序算法函数*/ int i, j; LineList temp; i = first; j = end; temp = r[i]; /*初始化*/ while (i < j) { while (i < j && temp.Key <= r[j].Key) j--; r[i] = r[j]; while (i < j && r[i].Key <= temp.Key) i++; r[j] = r[i]; } r[i] = temp; if (first < i - 1) QuickSort(r, first, i - 1); /*对左侧分区域进行快速排序*/ if (i + 1 < end) QuickSort(r, i + 1, end); /*对右侧分区域进行快速排序*/ }这个呢
要让这段代码输出排序的过程,可以在排序过程中添加一些输出语句。具体修改如下:
```
void QuickSort(LineList r[], int first, int end) { /*快速排序算法函数*/
int i, j;
LineList temp;
i = first;
j = end;
temp = r[i]; /*初始化*/
while (i < j)
{
while (i < j && temp.Key <= r[j].Key) j--;
r[i] = r[j];
while (i < j && r[i].Key <= temp.Key) i++;
r[j] = r[i];
}
r[i] = temp;
// 添加输出语句,输出每次排序后的结果
printf("第 %d 次排序结果:", i-1);
for(int k=first; k<=end; k++){
printf("%d ", r[k].Key);
}
printf("\n");
if (first < i - 1) QuickSort(r, first, i - 1); /*对左侧分区域进行快速排序*/
if (i + 1 < end) QuickSort(r, i + 1, end); /*对右侧分区域进行快速排序*/
}
```
这样修改后,每次排序后都会输出排序结果。同时,还会输出排序进行的次数。
对序列(503,87,512,61,908,170,897,275,653,426)使用快速排序(以第1个记录为枢轴)算法进行排序,补充函数,输出最后的排序结果。函数为:// 快速排序 #include "stdio.h" #define MAXSIZE 20 //顺序表的最大长度 typedef struct { int key; char otherinfo; }ElemType; //顺序表的存储结构 typedef struct { ElemType r[20]; //存储空间的基地址 int length; //顺序表长度 }SqList; //顺序表类型 void show(SqList L) { int i; for(i=1;i<=L.length;i++) printf("%4d",L.r[i].key); printf("\n"); } int Partition(SqList &L,int low,int high) { //对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置 //*************************************** //**************************************** }//Partition void QSort(SqList &L,int low,int high) { //调用前置初值:low=1; high=L.length; //对顺序表L中的子序列L.r[low..high]做快速排序 int pivotloc; if(low<high) { //长度大于1 pivotloc=Partition(L,low,high); //将L.r[low..high]一分为二,pivotloc是枢轴位置 QSort(L,low,pivotloc-1); //对左子表递归排序 QSort(L,pivotloc+1,high);//对右子表递归排序 } }//QSort void QuickSort(SqList &L) { //对顺序表L做快速排序 QSort(L,1,L.length); } //QuickSort void main() { SqList L; L.r[1].key=503; L.r[2].key=87; L.r[3].key=512; L.r[4].key=61; L.r[5].key=908; L.r[6].key=170; L.r[7].key=897; L.r[8].key=275; L.r[9].key=653; L.r[10].key=426; L.length=10; QuickSort(L); printf("快速排序后的结果为:"); show(L); }
下面是代码实现:
```c
#include "stdio.h"
#define MAXSIZE 20 //顺序表的最大长度
typedef struct {
int key;
char otherinfo;
}ElemType; //顺序表的存储结构
typedef struct {
ElemType r[MAXSIZE]; //存储空间的基地址
int length; //顺序表长度
}SqList; //顺序表类型
void show(SqList L) {
int i;
for(i=1;i<=L.length;i++)
printf("%4d",L.r[i].key);
printf("\n");
}
int Partition(SqList &L,int low,int high) {
//对顺序表L中的子表r[low..high]进行一趟排序,返回枢轴位置
int pivotkey=L.r[low].key; //将子表的第一个记录作为枢轴记录
while(low<high) { //从表的两端交替地向中间扫描
while(low<high&&L.r[high].key>=pivotkey) high--;
L.r[low]=L.r[high]; //将比枢轴记录小的记录移到低端
while(low<high&&L.r[low].key<=pivotkey) low++;
L.r[high]=L.r[low]; //将比枢轴记录大的记录移到高端
}
L.r[low].key=pivotkey; //枢轴记录到位
return low; //返回枢轴位置
}
void QSort(SqList &L,int low,int high) {
//调用前置初值:low=1; high=L.length;
//对顺序表L中的子序列L.r[low..high]做快速排序
int pivotloc;
if(low<high) { //长度大于1
pivotloc=Partition(L,low,high); //将L.r[low..high]一分为二,pivotloc是枢轴位置
QSort(L,low,pivotloc-1); //对左子表递归排序
QSort(L,pivotloc+1,high);//对右子表递归排序
}
}
void QuickSort(SqList &L) {
//对顺序表L做快速排序
QSort(L,1,L.length);
}
void main() {
SqList L;
L.r[1].key=503;
L.r[2].key=87;
L.r[3].key=512;
L.r[4].key=61;
L.r[5].key=908;
L.r[6].key=170;
L.r[7].key=897;
L.r[8].key=275;
L.r[9].key=653;
L.r[10].key=426;
L.length=10;
QuickSort(L);
printf("快速排序后的结果为:");
show(L);
}
```
输出结果为:
```
快速排序后的结果为: 61 87 170 275 426 503 653 897 908 512
```