Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的动态数组升序排序_C_Arrays_Dynamic - Fatal编程技术网

C语言中的动态数组升序排序

C语言中的动态数组升序排序,c,arrays,dynamic,C,Arrays,Dynamic,我正在尝试对动态数组列表进行排序,但它就是不起作用,我不知道该怎么做。这就是我到目前为止所做的: void ascending_sort(Controller* ctrl) { int i,j; Cost* aux; DynamicVector* c=getAllCosts(ctrl); for(i=0;i<getLen(c)-1;i++) { Cost* cost; for(j=i+1;j<ge

我正在尝试对动态数组列表进行排序,但它就是不起作用,我不知道该怎么做。这就是我到目前为止所做的:

    void ascending_sort(Controller* ctrl)
    {
    int i,j;
    Cost* aux;
    DynamicVector* c=getAllCosts(ctrl);
    for(i=0;i<getLen(c)-1;i++)
    {
        Cost* cost;
        for(j=i+1;j<getLen(c);j++)
        {
            if(cost[i].sum < cost[j].sum)
            {
                    aux=cost[i]; //error
                cost[i]=cost[j];
                cost[j]=aux; //error
            }
        }
    }
}
我怎样才能修好它?当我宣布“成本*aux”时,我认为我做错了什么。我希望你能帮助我

编辑:我更新了排序功能的新代码。现在它不打印我想要的。它只打印“成本1”而不打印其他内容,然后我收到一个“结束程序”窗口,停止所有操作。有什么问题吗

这是新的排序算法:

void ascending_sort(Controller* ctrl)
        {
        int i,j;
        DynamicVector* c=getAllCosts(ctrl);
        for(i=0;i<getLen(c)-1;i++)
        {
            Cost* cost=getElementAtPosition(c,i); //returns element on position
            for(j=i+1;j<getLen(c);j++)
            {
                if(cost[i].sum < cost[j].sum)
                {
                const Cost  aux=cost[i];
                            cost[i]=cost[j];
                            cost[j]=aux;
                }
            }
        }
    }

是的,当您需要实际值时,您正在使用指向
成本的指针。以下是最内层的作用域:

const Cost aux = cost[i];
cost[i] = cost[j];
cost[j] = aux;
请注意,我没有看到(或理解)
cost
、一个
cost
数组和
c
之间的关系,后者属于
DynamicVector*
类型,并且在循环中使用其长度


另外,你的排序算法不是很好;您应该只使用
qsort()

而不是
Cost
作为
aux

Cost
,修改后的排序代码更好,但可能做出了不必要的假设:

void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost = getElementAtPosition(c,i);
        for (int j = i+1; j < getLen(c); j++)
        {
            if (cost[i].sum < cost[j].sum)
            {
                const Cost aux = cost[i];
                cost[i] = cost[j];
                cost[j] = aux;
            }
        }
    }
}
或者,也许:

void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost_i = getElementAtPosition(c, i);
        for (int j = i+1; j < getLen(c); j++)
        {
            Cost *cost_j = getElementAtPosition(c, j);
            if (cost_i->sum < cost_j->sum)
            {
                setElementAtPosition(c, i, cost_j);
                setElementAtPosition(c, j, cost_i);
            }
        }
    }
}
无效升序排序(控制器*ctrl)
{
DynamicVector*c=getAllCosts(ctrl);
对于(inti=0;i总和<成本->总和)
{
setElementAtPosition(c、i、cost_j);
setElementAtPosition(c、j、cost_i);
}
}
}
}

成本未初始化,应为c?欢迎使用堆栈溢出。请在不久前阅读这篇文章。还请注意,修改问题使相关答案变得无关是不可接受的。您应该保留原始代码,指出在最新版本中所做的更改,但不应完全更改代码以使答案不相关。@JonathanLeffler谢谢!我再次编辑了这篇文章。你现在能帮我吗?在for循环中,我使用的是c的长度(这是一个DynamicVector*类型,包含我输入的所有成本:Cost1、Cost2、Cost3等)。我不知道什么是qsort(),我需要一个冒泡排序算法。:)@用户1849859:你喜欢缓慢而稳定,而不是快速而高效?冒泡排序很慢,O(N*N),而
qsort()
将是O(N log N)。当阵列很大时,差异真的很重要。气泡排序将花费大约100倍于
qsort()
对1000条记录进行排序的时间。
const Cost aux = cost[i];
cost[i] = cost[j];
cost[j] = aux;
void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost = getElementAtPosition(c,i);
        for (int j = i+1; j < getLen(c); j++)
        {
            if (cost[i].sum < cost[j].sum)
            {
                const Cost aux = cost[i];
                cost[i] = cost[j];
                cost[j] = aux;
            }
        }
    }
}
void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost_i = getElementAtPosition(c, i);
        for (int j = i+1; j < getLen(c); j++)
        {
            Cost *cost_j = getElementAtPosition(c, j);
            if (cost_i->sum < cost_j->sum)
                swapElements(c, i, j);
        }
    }
}
void ascending_sort(Controller* ctrl)
{
    DynamicVector *c = getAllCosts(ctrl);
    for (int i = 0; i < getLen(c)-1; i++)
    {
        Cost *cost_i = getElementAtPosition(c, i);
        for (int j = i+1; j < getLen(c); j++)
        {
            Cost *cost_j = getElementAtPosition(c, j);
            if (cost_i->sum < cost_j->sum)
            {
                setElementAtPosition(c, i, cost_j);
                setElementAtPosition(c, j, cost_i);
            }
        }
    }
}