PTA 数据结构与算法 7-37 模拟EXCEL排序

第一次写文章 不喜勿喷~
写这个是因为自己在学习数据结构的时候去网上找的答案大部分都是C++的 但是没有C的 这让用C写的我非常崩溃 所以在写完之后想要把自己的C代码和大家分享一下
下面进入正题

7-37 Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:

输入的第一行包含两个正整数N(≤10
​5
​​ ) 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:

在N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入样例:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
输出样例:

000001 Zoe 60
000007 James 85
000010 Amy 90

这道题就是使用快速排序就可以了 但是需要注意一点的是 这里要使用三数取中法来对付N最大的C=1,C=2 的情况 ,不知道是不是给出的测试数据的顺序对和逆序对个数相近 我在用非三数取中的快速排序的测试情况下给出了TLE 但是C=3 的却是AC
下面放失败的图
在这里插入图片描述
以及代码

#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct Student{
char name[9];
int ID;
int score;
};

int cmp(struct Student *s1,struct Student *s2,int way);
void QuickSort(struct Student *student,int left,int right,int way);

int main(void)
{
    int N,C;    //记录条数  排序方式
    int i;
    fscanf(stdin,"%d %d",&N,&C);
    struct Student *student;
    student=(struct Student *)malloc(N*sizeof(struct Student));

    for(i=0;i<N;i++)
      fscanf(stdin,"%d %s %d",&student[i].ID,student[i].name,&student[i].score);

    QuickSort(student,0,N-1,C);

    for(i=0;i<N;i++)
      printf("%06d %s %d\n",student[i].ID,student[i].name,student[i].score);

    free(student);

    return 0;
}

int cmp(struct Student *s1,struct Student *s2,int way)
{
    int result=0;

    if(1==way)
      result=s2->ID-s1->ID;
    else if(2==way){
      result=strcmp(s1->name,s2->name);
      result*=-1;
    }
    else
      result=s2->score-s1->score;

    if(!result)
      result=s2->ID-s1->ID;

    return result;
}

void QuickSort(struct Student *student,int left,int right,int way)
{
    if(left>=right)
      return ;

    int i,j;
    struct Student temp;
    i=left;
    j=right;

    while(i<j){
      while(i<j&&cmp(&student[left],&student[j],way)>0)
        j--;
      while(i<j&&cmp(&student[left],&student[i],way)<=0)
        i++;
      if(i<j){
        temp=student[i];
        student[i]=student[j];
        student[j]=temp;
      }
    }

    if(left!=i){
      temp=student[left];
      student[left]=student[i];
      student[i]=temp;
    }

    QuickSort(student,left,i-1,way);
    QuickSort(student,i+1,right,way);

    return ;
}

然后在翻阅别人的代码是发现用得是C++中的 qsort 函数 去查阅代码的时候发现它是先进行1.5*log2(n)次的快排,如果还没排完,就使用堆排
这两种排序的复杂度相等,然后我就想到了堆栈溢出的问题 ,当基本有序的时候快排会造成堆栈的大量堆积,这样可能造成堆栈溢出 。然后一看数据量是10^5 量级 然后用该量级的完全有序数据去跑了下 给了段错误 于是判断就是堆栈溢出的错误 采用三数取中后就AC掉了

#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct Student{
char name[9];
int ID;
int score;
};

int cmp(struct Student *s1,struct Student *s2,int way);
void QuickSort(struct Student *student,int left,int right,int way);

int main(void)
{
    int N,C;    //记录条数  排序方式
    int i;
    fscanf(stdin,"%d %d",&N,&C);
    struct Student *student;
    student=(struct Student *)malloc(N*sizeof(struct Student));

    for(i=0;i<N;i++)
      fscanf(stdin,"%d %s %d",&student[i].ID,student[i].name,&student[i].score);

    QuickSort(student,0,N-1,C);

    for(i=0;i<N;i++)
      printf("%06d %s %d\n",student[i].ID,student[i].name,student[i].score);

    free(student);

    return 0;
}

int cmp(struct Student *s1,struct Student *s2,int way)
{
    int result=0;

    if(1==way)
      result=s2->ID-s1->ID;
    else if(2==way)
      result=strcmp(s2->name,s1->name);
    else
      result=s2->score-s1->score;

    if(!result)
      result=s2->ID-s1->ID;

    return result;
}

void QuickSort(struct Student *student,int left,int right,int way)
{
    if(left>=right)
      return ;

    int i,j,mid;
    int index;

    struct Student temp;
    i=left;
    j=right;
    mid=left+((right-left)>>1);

    if(3==way)
      index=left;              // 按成绩排序的时候 因为最多只有100次的分层 故可以不用三数取中 这样节省了比较的时间
    else{
      if(cmp(&student[left],&student[right],way)>0){
        if(cmp(&student[left],&student[mid],way)>0){
          if(cmp(&student[mid],&student[right],way)>0)
            index=mid;
          else
            index=right;
        }
        else
          index=left;
      }
      else{
        if(cmp(&student[mid],&student[right],way)>0)
          index=right;
        else{
          if(cmp(&student[mid],&student[left],way)>0)
            index=mid;
          else
            index=left;
        }
      }                            //三数取中法
    }

    while(i<j){
      while(i<j&&cmp(&student[index],&student[j],way)>0)
        j--;
      while(i<j&&cmp(&student[index],&student[i],way)<=0)
        i++;
      if(i<j){
        temp=student[i];
        student[i]=student[j];
        student[j]=temp;
      }
    }

    if(index!=i){
      temp=student[index];
      student[index]=student[i];
      student[i]=temp;
    }

    QuickSort(student,left,i-1,way);
    QuickSort(student,i+1,right,way);

    return ;
}

在这里插入图片描述

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值