【发布时间】:2012-11-02 23:55:02
【问题描述】:
如何使用冒泡排序(Student::name 上的 student[] 降序排序并排序 classes[] Class::title 上的数组?
struct Class
{
string title;
int units;
char grade;
};
struct Student
{
string name;
double gpa;
Class classes[500];
};
主要:
Student students[SIZE];
我正在尝试对一个结构数组进行排序,每个结构都包含一个结构数组,这些结构也需要使用冒泡排序进行排序。我的排序功能粘贴在下面。它没有正确排序,而是根据标题正确地对结构类 [] 的内部数组进行排序,并在 for 循环的第一次迭代中正确地对外部数组 st[] 进行排序。由于在第二次迭代中 st[] 的元素已被交换,因此第一个元素未排序 b/c currentStu 现在设置为数组中的第二个元素。
void sort_name(Student st[], int numValues)
{
int currentStu = 0;
int currentClass = 0;
for(currentStu = 0; currentStu < numValues; currentStu++)
{
for(currentClass = 0; st[currentStu].classes[currentClass].title != ""; currentClass++)
{
bubbleUpClass(st, currentClass, currentStu);
}
bubbleUpLastName(st, currentStu, numValues - 1);
}
}
【问题讨论】:
-
可怜这个穷学生居然上了500节课!
-
大声笑,我正在努力尽快完成学业(每季度 5-6 节课)。这就是我在编写代码时的感受 :)
-
考虑到一个 9 年的博士课程需要 (5classes/term * 3terms/year * 9years) 最多 145 个课程,我很想知道这个学生在学习什么,以及有多大他们需要赢的彩票才能偿还学生贷款。
-
@JonathanLeffler 谢谢;我今天一直盯着代码方式太久了。 135 类。 (*别告诉我我也吃过那个;)
-
除非你的bubbleUpStudent的算法有漏洞,否则应该在
students的槽[0]中的学生应该在你第一次通过后回家;在两次通过后的 slot[0..1] 中,等等。您的气泡功能是否将项目推到桌子的 end ?如果是这样,它可能是倒退的。您的调用也可能是bubbleUpLastName(st, currentStu, numValues - currentStu);
标签: c++ sorting bubble-sort