使用结构体数组在 C 中实现排序算法时出现问题
好吧,这是我的小问题,首先是我的代码:
struct alumn {
char name[100];
char lastname[100];
int par;
int nota;
};
typedef struct alumn 校友;
int bubble(alum **arr, int length)
{
整数 i,j;
校友*临时;
for (i=0; i<=length-2; i++) {
for (j=i+1; j<=length-1;j++) {
if ((*arr)[i].nota > (*arr)[j].nota) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main(int argc, char **argv)
{
校友*校友;
... here goes some other code ...
bubble(&alumns,totalAlumns);
return 0;
}
我的问题是这个算法没有对任何东西进行排序。我在进行交换时遇到了困难,我尝试了所有方法,但没有任何效果:(。有什么帮助吗???
Well here is my little problem, first my code:
struct alumn {
char name[100];
char lastname[100];
int par;
int nota;
};
typedef struct alumn alumn;
int bubble(alumn **arr, int length) { int i,j; alumn *temp;
for (i=0; i<=length-2; i++) {
for (j=i+1; j<=length-1;j++) {
if ((*arr)[i].nota > (*arr)[j].nota) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main(int argc, char **argv)
{
alumn *alumns;
... here goes some other code ...
bubble(&alumns,totalAlumns);
return 0;
}
My problem is that this algorith is not sorting anything. I'm having a hard time doing the swap, i tried everything but nothing works :( . Any help???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,您将alumn结构数组与指向alumm结构的指针数组混淆了。
气泡逻辑表达了指针数组,主函数似乎用结构数组来调用它。
由于 alumn 结构体的大小,对指针执行冒泡排序可能会更有效,因为每次交换所需的数据移动要少得多(一个指针的 3 个副本,每个副本几个字节,而 3 个 alumn 副本)结构体,每个 200+ 字节!)。
我建议您修改 main() 函数的逻辑(未在问题片段中显示)以引入这样一个指向实际 alumn 结构的指针数组。 (当然,这个指针数组不会让您免于分配结构本身,整体分配(在结构数组中)或单独分配。
根据您的坚持,我在这里暗示 main 看起来如何生成指针数组可通过气泡使用(气泡保持不变)。
顺便说一句,我将 alumns 声明为
alumn *alumns[]
,这更容易显示意图使用。这与alumn **alumns
是一样的。或者,如果您希望像以前一样保留原始的 alumns 变量,则可能需要的只是在调用 bubble() 之前。
应该强调的一件事是,无论其起源如何,数组都会传递给 bubble( ) 排序正常,但要使用它,您需要取消引用各个指针。
对于您打算使用的旧数组,如
alumns[123].lastname
,您现在需要alumns[123]->lastname
(或>ap_alumns[123]->lastname
(如果您使用第二个版本)。Apparently you're confusing an array of alumn structs with an array of pointers to alumm struct.
The Bubble logic expexts the array of pointers, whereby the main function seems to call it with an array of structs.
Because of the size of the alumn struct, it is probably more efficient to perform the bubble sort on pointers, since each swap will require much less moving around of data (3 copies of one pointer a few bytes each, vs. 3 copies of alumn struct, 200+ bytes each!).
I suggest you modify the logic (not shown in snippet of question) of the main() function to introduce such an array of pointers to the actual alumn structs. (Of course this array of pointers won't spare you from also allocating the structs themselves, en block (in an array of structs) or individually.
Upon your insistence I'm hinting here how main could look like to produce an array of pointer usable by bubble (bubble stays unchanged).
BTW, I declare alumns as
alumn *alumns[]
which shows the intent use more readily. this is the same thing asalumn **alumns
.Alternatively if you wish to keep the original alumns variable as previously, all that may be needed is something like that, just before the call to bubble()
One thing that should be stressed is that regardless of its genesis, the array passed to bubble() gets sorted ok, but to use it you need to dereference the individual pointers.
Whereby with the old array you intended to use as in say
alumns[123].lastname
, you'll now needalumns[123]->lastname
(orap_alumns[123]->lastname
if you use the second version).您的代码不起作用,因为您有一个结构数组而不是指针数组。
当您尝试交换两个结构时, = 运算符不知道该怎么做。您必须一一复制结构体的字段才能使其正常工作。
如果您有一个指向 alumn 结构实例的指针数组。那么代码就可以工作,因为您正在分配指针。指针基本上是一个数字,并且 = 知道如何复制数字。
Your code does not work because you have an array of structs instead of an array of pointers.
When you try to swap two structs, the = operator does not know what to do. You have to copy the fields of the struct one by one for that to work.
If you had an array of pointers to instances of the alumn struct instead. THen the code would work, since you are assigning pointers. A pointer is basically a number and = knows how to copy a number.
您的代码编写得就像有一个指针数组一样,但遗漏了代码的重要部分(即
...这里还有一些其他代码...
),所以我们不能查看您如何设置要排序的内容。如果你有一个指针数组,这行代码:应该是:
原因是, (*arr) 获取列表中的第一个指针,然后 (*arr)[i] 获取内存中的第 i 个项目指针(无意义,因为每个指针应该指向一个项目)。第二个语法转到指针数组中的第 i 个指针,并取消引用它以获取该值。
也许这个插图会有所帮助:
Your code is written as if you have an array of pointers, but have left out an important part of your code (i.e.,
... here goes some other code ...
) so we can't see how you've set up the thing to be sorted. If you have an array of pointers, this line of code:should be:
The reason is, (*arr) gets the first pointer in the list, then (*arr)[i] gets the i'th item in memory after that pointer (meaningless since each pointer should point to one item). The 2nd syntax goes to the i'th pointer in the pointer array, and dereferences it to get that value.
Maybe this illustration will help: