7.2.c

来自「自己写的数据结构课程程序」· C语言 代码 · 共 66 行

C
66
字号
#include<stdio.h>
#include<string.h>
#define MAXSIZE 20
struct student
{char name[20];
 int score;
};
struct SqList
{struct student r[MAXSIZE+1];
 int length;
};
struct SqList L;
void InitSqList()
{int i;
 printf("input the number of data:\n");
 scanf("%d",&L.length);
 printf("input the data:\n");
 for(i=1;i<=L.length;i++)
   {scanf("%s",L.r[i].name);
    scanf("%d",&L.r[i].score);
   }
}
int Partition(int low,int high)
{int score;
 strcpy(L.r[0].name,L.r[low].name);
 L.r[0].score=L.r[low].score;
 score=L.r[low].score;
 while(low<high)
   {while(low<high&&L.r[high].score<=score)
      high--;
    strcpy(L.r[low].name,L.r[high].name);
    L.r[low].score=L.r[high].score;
    while(low<high&&L.r[low].score>=score)
      low++;
    strcpy(L.r[high].name,L.r[low].name);
    L.r[high].score=L.r[low].score;
   }
 strcpy(L.r[low].name,L.r[0].name);
 L.r[low].score=L.r[0].score;
 return low;
}
void QSort(int low,int high)
{int pivotloc;
 if(low<high)
   {pivotloc=Partition(low,high);
    QSort(low,pivotloc-1);
    QSort(pivotloc+1,high);
   }
}
void PrintSqList()
{int n=1,i;
 printf("\n");
 for(i=1;i<=L.length;i++)
   {printf("%d %s %d\n",n,L.r[i].name,L.r[i].score);
    if(L.r[i].score>L.r[i+1].score)
      n++;
   }
}
void main(void)
{InitSqList();
 QSort(1,L.length);
 PrintSqList();
 getchar();
 getchar();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?