C 气泡分选动态结构

C 气泡分选动态结构,c,struct,logic,C,Struct,Logic,我已经成功地创建了一个动态结构数组,但我似乎无法按课程名称对结构进行排序。我的结构减速看起来像 struct info { char* student; char* courseName; int grade; }; 我的排序函数有如下的偏差,它的类型是指针,因为它是使用动态内存分配创建的 void sort(struct info **array,int idx); 排序前打印生成结构的学生、课程名称和年级成员 Khai IE 3301 69 Ashley MATH 1426 59 Ali

我已经成功地创建了一个动态结构数组,但我似乎无法按课程名称对结构进行排序。我的结构减速看起来像

struct info {
char* student;
char* courseName;
int grade;
};
我的排序函数有如下的偏差,它的类型是指针,因为它是使用动态内存分配创建的

void sort(struct info **array,int idx);
排序前打印生成结构的学生、课程名称和年级成员

Khai IE 3301 69
Ashley MATH 1426 59
Alisaad CSE 1325 31
August CSE 1325 55
Ethan CSE 1320 92
Emily CSE 1310 26
Ahad IE 3301 32
Duke PHYS 1444 29
Duke MATH 2425 90
Ethan MATH 2425 42
Emily MATH 1426 16
Duke MATH 1426 28
Emily CSE 1325 0
Ravindra PHYS 1444 30
August CSE 1325 87
Ravindra IE 3301 51
Ravindra CSE 1310 55
Emily MATH 2425 1
August PHYS 1443 12
所以我要做的是对这些条目进行冒泡排序,在排序的最后,我的结构数组可以有如下形式..按字母顺序和数字顺序排序

CSE 1310 Emily
CSE 1310 Ravindra
CSE 1320 Ethan
CSE 1325.....
IE 3301.....
我不太擅长向函数发送指针,所以我相信这就是我搞砸的地方。也许我没有像我想的那样引用结构数组的成员,我正在错误地交换条目,但这里是我对数组排序的代码块。我在顶部包含了string.h,函数正在接收一个正确构建的结构数组,正如我在主代码块中打印的那样,但是排序会把数组搞得一团糟

void sort(struct info **array,int idx) */idx is the size of the finished structure array*/
{
    int unsorted, i;
    struct info temp;

    do {
        unsorted = 0;
        for(i=0; i < idx - 1;i++)
            if(strcmp(array[i]->courseName,array[i+1]->courseName) > 0)
                {
                    temp = *array[i];
                    array[i] = array[i+1];
                    *array[i+1] = temp;
                    unsorted = 1;
                }
    }
    while(unsorted);
    printf("\n");

    int k = 0;
    for(k = 0; k < idx; k++)
        printf("%s %s \n",array[k]->courseName,array[k]->student);
}

您需要:
*数组[i]=*数组[i+1]


这是因为在没有*的情况下,您将数组[i+1]的地址传递给数组[i],因此它们最终指向相同的值

您是想交换结构值还是只交换指向结构的指针

如果您想要交换结构值(这是您的代码似乎想要做的),那么您可能需要:

                temp = *array[i];
                *array[i] = *array[i+1];
                *array[i+1] = temp;
                unsorted = 1;

不要交换结构;交换指针。你的开场白声称你有一个“动态的结构数组”,但你没有。您有一个指针的动态数组,每个指针都指向一个结构(动态与否,它并不真正相关)

记住这一点的最简单方法是注意,要交换的临时对象应该是从阵列中移除的一级间接寻址。由于数组是
struct info**
,因此临时数组应该是
struct info*
;不
struct info

我还冒昧地适当调整了我在问题下面的一般性评论中提到的顶部索引缩减,这应该在每次传递时完成,并修复函数声明行上的不正确评论(应该以
/*
开始,而不是
*/
。生成的代码如下所示:

/*idx is the size of the finished structure array*/
void sort(struct info const **array, unsigned int idx)
{
    unsigned int swapped = 1, top = idx, i;
    while (top-- && swapped)
    {
        swapped = 0;
        for(i=0; i < top; ++i)
        {
            if (strcmp(array[i]->courseName,array[i+1]->courseName) > 0)
            {
                struct info const *temp = array[i];
                array[i] = array[i+1];
                array[i+1] = temp;
                swapped = 1;
            }
        }
    }

    /* report finale */
    for(i=0; i < idx; i++)
        printf("%s %s\n", array[i]->courseName,array[i]->student);
}
/*idx是完成的结构数组的大小*/
无效排序(结构信息常量**数组,无符号整数idx)
{
无符号整数交换=1,top=idx,i;
while(顶部--&&swapped)
{
交换=0;
对于(i=0;icourseName,数组[i+1]->courseName)>0)
{
结构信息常量*temp=数组[i];
数组[i]=数组[i+1];
阵列[i+1]=温度;
交换=1;
}
}
}
/*报告结尾*/
对于(i=0;icourseName,数组[i]->student);
}

这是动态指针数组上的经典冒泡排序。

尝试了这个方法,但在说出struct中的不兼容类型时出错info@ZachSantiago--您需要显示创建数组的代码。对于是否有一个结构数组或指针数组,似乎存在一些混淆。这很有效,请您解释一下y向结构值添加星点?指针对我来说很难,thxarray是struct info**。它指向一个指针数组。这些指针指向struct info类型的项。因此数组[i]是struct info*类型的指针。temp是struct info类型的指针,因此您可以取消引用(*array[i])要获取数组[i]指向的项的值并将其复制到temp中,则需要复制数组[i]和数组[i+1]指向的结构的内容,因此您需要取消引用。根据您创建数组的方式,也可能只交换存储在数组中的指针,而不是数组中元素指向的数据。格式良好,呈现良好的问题。相关(可以,但仍然很小):在每次迭代中,您也不会减少冒泡停止。这在某种程度上是冒泡排序的要点。每次通过时,“获胜者”冒泡都会到达当前的顶部位置,其索引(
idx
)在下一次传递时减少1。在没有交换检测时提前退出的逻辑是当场打开的,顺便说一句,这让我有点惊讶,你错过了每次传递时的停止索引减少。+1但我认为你想在
while
语句中预减量
top
,因为如果top是1,内部循环无论如何都不会运行。@user3386109 the后减量是故意的。我知道内循环不会在输入为
1
时运行,但同样地,外循环也不会在输入为
0
时运行,并且不需要单独的
if
在循环之前进行检测。
/*idx is the size of the finished structure array*/
void sort(struct info const **array, unsigned int idx)
{
    unsigned int swapped = 1, top = idx, i;
    while (top-- && swapped)
    {
        swapped = 0;
        for(i=0; i < top; ++i)
        {
            if (strcmp(array[i]->courseName,array[i+1]->courseName) > 0)
            {
                struct info const *temp = array[i];
                array[i] = array[i+1];
                array[i+1] = temp;
                swapped = 1;
            }
        }
    }

    /* report finale */
    for(i=0; i < idx; i++)
        printf("%s %s\n", array[i]->courseName,array[i]->student);
}