我不知道这是否是基本的,但我只知道如何按升序排序。
system("cls");
if (headptr != (struct online*)NULL)
{
currentptr = headptr;
for(; currentptr->ptrnext != NULL; currentptr = currentptr->ptrnext)
{
for(newptr = currentptr->ptrnext; newptr != (struct online*)NULL; newptr = newptr->ptrnext)
{
if(currentptr->score > newptr->score)
{
int temp = currentptr->score;
currentptr->score = newptr->score;
newptr->score = temp;
}
}
}
}
最佳答案
是的,这很容易。只需将运算符从“>”更改为“我没有测试代码,但如果原始代码段正常工作,则只需执行以下操作:
system("cls");
if (headptr != (struct online*)NULL)
{
currentptr = headptr;
for(; currentptr->ptrnext != NULL; currentptr = currentptr->ptrnext)
{
for(newptr = currentptr->ptrnext; newptr != (struct online*)NULL; newptr = newptr->ptrnext)
{
if(currentptr->score < newptr->score)
{
int temp = currentptr->score;
currentptr->score = newptr->score;
newptr->score = temp;
}
}
}
}
在这里,您可以观看可视化不同排序算法的动画,这可以帮助您深入了解气泡排序的工作方式:
Sorting Algorithms - Visualized