C语言排序 选择排序和冒泡排序
voidrank(NODE*src,NODE*dest){strcpy(dest->num,src->num);strcpy(dest->grade_number,src...
void rank(NODE*src,NODE*dest)
{
strcpy(dest->num,src->num);
strcpy(dest->grade_number,src->grade_number);
strcpy(dest->name,src->name);
strcpy(dest->sex,src->sex);
strcpy(dest->ID_num,src->ID_num);
strcpy(dest->phonenumber,src->phonenumber);
strcpy(dest->integration,src->integration);
}
void sort(NODE*p)
{NODE*h=p;
NODE*records[100];
NODE tmps;
int num=0,j,i;
char c;
while(p!=NULL)
{ records[num]=p;
p=p->next;
num++;
}
printf("请输入排序方式:");
printf("1.编号;2.等级;3.姓名;4.性别;5.身份证号;6.电话号;7.积分;0.返回\n");
getchar();
c=getchar();
for(i=0;i<num-1;i++)
{
for(j=i+1;j<num;j++)
{
if((c=='1'&&strcmp(records[i]->num,records[j]->num)>0)||
(c=='2'&&strcmp(records[i]->grade_number,records[j]->grade_number)>0)||
(c=='3'&&strcmp(records[i]->name,records[j]->name)>0)||
(c=='4'&&strcmp(records[i]->sex,records[j]->sex)>0)||
(c=='5'&&strcmp(records[i]->ID_num,records[j]->ID_num)>0)||
(c=='6'&&strcmp(records[i]->phonenumber,records[j]->phonenumber)>0)||
(c=='7'&&strcmp(records[i]->integration,records[j]->integration)>0))
{
rank(records[i],&tmps);
rank(records[j],records[i]);
rank(&tmps,records[j]);
}
}
}
printf("排序已经完成\n");
}
把以上的冒泡排序改为其他方式的排序,比如选择排序或是快速排序 展开
{
strcpy(dest->num,src->num);
strcpy(dest->grade_number,src->grade_number);
strcpy(dest->name,src->name);
strcpy(dest->sex,src->sex);
strcpy(dest->ID_num,src->ID_num);
strcpy(dest->phonenumber,src->phonenumber);
strcpy(dest->integration,src->integration);
}
void sort(NODE*p)
{NODE*h=p;
NODE*records[100];
NODE tmps;
int num=0,j,i;
char c;
while(p!=NULL)
{ records[num]=p;
p=p->next;
num++;
}
printf("请输入排序方式:");
printf("1.编号;2.等级;3.姓名;4.性别;5.身份证号;6.电话号;7.积分;0.返回\n");
getchar();
c=getchar();
for(i=0;i<num-1;i++)
{
for(j=i+1;j<num;j++)
{
if((c=='1'&&strcmp(records[i]->num,records[j]->num)>0)||
(c=='2'&&strcmp(records[i]->grade_number,records[j]->grade_number)>0)||
(c=='3'&&strcmp(records[i]->name,records[j]->name)>0)||
(c=='4'&&strcmp(records[i]->sex,records[j]->sex)>0)||
(c=='5'&&strcmp(records[i]->ID_num,records[j]->ID_num)>0)||
(c=='6'&&strcmp(records[i]->phonenumber,records[j]->phonenumber)>0)||
(c=='7'&&strcmp(records[i]->integration,records[j]->integration)>0))
{
rank(records[i],&tmps);
rank(records[j],records[i]);
rank(&tmps,records[j]);
}
}
}
printf("排序已经完成\n");
}
把以上的冒泡排序改为其他方式的排序,比如选择排序或是快速排序 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询