新手小白1640 2020-06-17 15:47 采纳率: 50%
浏览 173
已采纳

一个快速排序,有错误,应该怎么改啊,感谢指正

#include
#include
#include
#define MAXSIZE 100

typedef struct Student{
char no[20];// 学号
char name[20];// 姓名
int price;// 成绩
}Student;
int sum=0;

typedef struct{
Student elem[MAXSIZE];//线性表占用的数组空间
int last;//最后一个元素的下标
}SeqList;

//快速排序(学号)

int Partition(SeqList &L,Student a[],int low,int high)
{
a[0]=a[low];
int pivotkey=a[low].no;
while(low {
while(low=pivotkey) --high;
a[low]=a[high];
while(low<high&&a[low].no<=pivotkey) ++low;
a[high]=a[low];
}
a[low]=a[0];
return low;
}

void QSort(Student a[],int low,int high)
{
if(low<high){
int pivotloc=Partition(L,a,low,high);
QSort(L,a,low,pivotloc-1);
QSort(L,a,pivotloc+1,high);
}
}
void QuickSort(SeqList &L)
{
if(L.last<1)
{
printf("空表!");
}
Student a[MAXSIZE];//排序专用临时数组
for(int i=1;i<=L.last;i++)
a[i]=L.elem[i];
QSort(L.elem,1,L.last);
for(i=0;i<L.last;i++)
{
printf("学号:%s 姓名:%s 成绩:%d\n\n",L.elem[i].no,L.elem[i].name,L.elem[i].price);
}
printf("\n");
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-06-17 16:04
    关注

    int pivotloc=Partition(L,a,low,high);
    这里调试下,分区是否正确

    for(int i=1;i<=L.last;i++)
    数组下标从0开始

    while(low {
    while(low=pivotkey) --high;
    这些代码都不完整

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程