准备m个箱子,先按低位分箱再按序号一次将各个非空箱子里的记录收集起来,再对新收集起来的元素依次按较高的位分箱,直到最高位。
分箱即将第s个关键字等于ti的全部记录装入第i个箱子里。
按最高位分箱后,按序号一次将各个非空箱子里的记录收集起来,得到的元素序列就是有序的。
Hash排序是在Hash查找的基础上演变而来。
对待排序列采用单调的Hash函数,并用链地址法处理冲突,最后用一定规则收集存储好的数据从而得到有序序列。
三、实验内容及要求
访问
首先设法将网站上的数据导入一个文本文件,然后用程序去读取该文件中的数据,对数据的处理可以自行选择排序算法。
用菜单选择排序依据(如输入A按上场时间排序,输入B按投篮率排序……),处理后的数据用fwrite方式写入新文件中。
四、实验方案或技术路线(只针对综合型和设计型实验)
通过在程序中对结构体中指针的定义、赋值和引用从而完成对链表中特定数据的引用,结合文件的使用、函数的调用,自行选择排序算法、编制算法完成指定程序的编制、调试和执行,并通过观测程序输出结果验证程序设计的正确性。
实验过程总结如下:
(1)选定一种排序算法,对该算法实现的思路进行分析
(2)编程实现选定的算法
(3)对所编写的程序进行调试
(4)对程序运行的结果进行截图
(5)对实验结果进行分析
五、实验原始记录(可附加页)
(程序设计类实验:
包括原程序、输入数据、运行结果、实验过程发现的问题及解决办法等;
分析与设计、软件工程类实验:
编制分析与设计报告,要求用标准的绘图工具绘制文档中的图表。
系统实施部分要求记录核心处理的方法、技巧或程序段;
其他实验:
包括实验输入数据,处理模型、输出数据及结果分析)
1、我选择的球队是火箭队,存放在文件read.txt文件中的内容如下:
2、源程序如下:
#include
#include
#include
typedefstructMatchData
{
charname[20];//球员
floattime;//时间
floatshoot;//投篮
floatthreepoint;//三分
floatfreethrow;//罚球
floatpreviousRebounds;//前篮板
floatbackRebounds;//后篮板
floattotalRebounds;//总篮板
floatassist;//助攻
floatsteal;//抢断
floatblockshot;//盖帽
floatturnover;//失误
floatfoul;//犯规
floatscore;//得分
}MatchData;
intLineNumStat(FILE*fp);
voidInitialPlayers(MatchData*players,intplayersNumber);
voidInputPlayersData(FILE*fp,MatchData*players,intplayersNumber);
voidShowMenu();
voidSortPlayersData(MatchData*players,intplayersNumber,intsortType);
voidSortByTime(MatchData*players,intplayersNumber);
voidSortByShoot(MatchData*players,intplayersNumber);
voidSortByThreepoint(MatchData*players,intplayersNumber);
voidSortByFreethrow(MatchData*players,intplayersNumber);
voidSortByPreviousRebounds(MatchData*players,intplayersNumber);
voidSortByBackRebounds(MatchData*players,intplayersNumber);
voidSortByTotalRebounds(MatchData*players,intplayersNumber);
voidSortByAssist(MatchData*players,intplayersNumber);
voidSortBySteal(MatchData*players,intplayersNumber);
voidSortByBlockshot(MatchData*players,intplayersNumber);
voidSortByTurnover(MatchData*players,intplayersNumber);
voidSortByFoul(MatchData*players,intplayersNumber);
voidSortByScore(MatchData*players,intplayersNumber);
voidOutputPlayersData(FILE*fp,MatchData*players,intplayersNumber);
intmain()
{
MatchData*players;
FILE*fpin,*fpout;//输入文件和输出文件指针
intlineNumber;//行数
intplayersNumber;//球员的人数
charinputFileName[256];
charoutputFileName[256];
intchoice;
printf("请输入存储输入数据的文件名:
\n");
scanf("%s",inputFileName);
if((fpin=fopen(inputFileName,"r"))==NULL)
{
printf("打开输入文件出错!
\n");
exit
(1);
}
printf("请输入存储输出数据的文件名:
\n");
scanf("%s",outputFileName);
if((fpout=fopen(outputFileName,"wb"))==NULL)
{
printf("打开输出文件出错!
\n");
exit
(2);
}
lineNumber=LineNumStat(fpin);
rewind(fpin);//文件位置指针回到文件头
playersNumber=lineNumber;
players=(MatchData*)malloc(playersNumber*sizeof(MatchData));
InitialPlayers(players,playersNumber);
InputPlayersData(fpin,players,playersNumber);
ShowMenu();
scanf("%d",&choice);
SortPlayersData(players,playersNumber,choice);
OutputPlayersData(fpout,players,playersNumber);
free(players);
fclose(fpin);
fclose(fpout);
return0;
}
//统计文本文件的行数
intLineNumStat(FILE*fp)
{
intlinenum=0;
charbuffer[100];
while(!
feof(fp))
{
fgets(buffer,100,fp);
linenum++;
}
returnlinenum;
}
//初始化球员的数据
voidInitialPlayers(MatchData*players,intplayersNumber)
{
inti;
MatchData*p;
for(i=0;i{
p=&players[i];
strcpy(p->name,"");
p->time=0;
p->shoot=0;
p->threepoint=0;
p->freethrow=0;
p->previousRebounds=0;
p->backRebounds=0;
p->totalRebounds=0;
p->assist=0;
p->steal=0;
p->blockshot=0;
p->turnover=0;
p->foul=0;
p->score=0;
}
}
//输入球员数据
voidInputPlayersData(FILE*fp,MatchData*players,intplayersNumber)
{
inti;
MatchData*p;
for(i=0;i{
p=&players[i];
fscanf(fp,"%s%f%f%%%f%%%f%%%f%f%f%f%f%f%f%f%f",p->name,&p->time,&p->shoot,&p->threepoint,&p->freethrow,&p->previousRebounds,&p->backRebounds,
&p->totalRebounds,&p->assist,&p->steal,&p->blockshot,&p->turnover,&p->foul,&p->score);
}
}
//显示菜单
voidShowMenu()
{
printf("请选择排序方式:
\n");
printf("1.按时间排序\n");
printf("2.按投篮排序\n");
printf("3.按三分排序\n");
printf("4.按罚球排序\n");
printf("5.按前篮板排序\n");
printf("6.按后篮板排序\n");
printf("7.按总篮板排序\n");
printf("8.按助攻排序\n");
printf("9.按抢断排序\n");
printf("10.按盖帽排序\n");
printf("11.按失误排序\n");
printf("12.按犯规排序\n");
printf("13.按得分排序\n");
printf("请输入选项:
");
}
//排序球员数据
voidSortPlayersData(MatchData*players,intplayersNumber,intsortType)
{
switch(sortType)
{
case1:
SortByTime(players,playersNumber);
break;
case2:
SortByShoot(players,playersNumber);
break;
case3:
SortByThreepoint(players,playersNumber);
break;
case4:
SortByFreethrow(players,playersNumber);
break;
case5:
SortByPreviousRebounds(players,playersNumber);
break;
case6:
SortByBackRebounds(players,playersNumber);
break;
case7:
SortByTotalRebounds(players,playersNumber);
break;
case8:
SortByAssist(players,playersNumber);
break;
case9:
SortBySteal(players,playersNumber);
break;
case10:
SortByBlockshot(players,playersNumber);
break;
case11:
SortByTurnover(players,playersNumber);
break;
case12:
SortByFoul(players,playersNumber);
break;
case13:
SortByScore(players,playersNumber);
break;
default:
printf("排序方式选择错误,程序即将退出。
\n");
break;
}
}
//按时间排序
voidSortByTime(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].time{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按投篮排序
voidSortByShoot(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].shoot{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按三分排序
voidSortByThreepoint(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].threepoint{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按罚球排序
voidSortByFreethrow(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].freethrow{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按前篮板排序
voidSortByPreviousRebounds(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].previousRebounds{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按后篮板排序
voidSortByBackRebounds(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].backRebounds{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按总篮板排序
voidSortByTotalRebounds(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].totalRebounds{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按助攻排序
voidSortByAssist(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatemp;
for(i=playersNumber-1;i>=1;i--)
{
for(j=0;j
{
if(players[j].assist{
temp=players[j];
players[j]=players[j+1];
players[j+1]=temp;
}
}
}
}
//按抢断排序
voidSortBySteal(MatchData*players,intplayersNumber)
{
inti,j;
MatchDatatem