📄 7.2.c
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -