1

I am trying to sort a Dynamic Array list but it just doesn't work and I don't understand how I should do it. This is what I've done so far:

    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
            }
        }
    }
}

Here's the struct:

typedef struct{
    char* day;
    char* type;
    int sum;
}Cost;

How can I fix it? I think I am doing something wrong when I declare "Cost* aux". I hope you can help me!

EDIT: I updated the new code for the sorting function. Now it doesn't print what I want. It prints 'Cost 1' and nothing else and then I receive an "End program" window that stops everything. What could be the problem?

This is the new sorting algorithm:

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;
                }
            }
        }
    }

This is the print function: //this function is in the console

void PrintCosts(Console* console)
{
    DynamicVector* CostList=getAllCosts(console->ctrl);
    if (getLen(CostList))
    {
        int i;
        for(i=0;i<getLen(CostList);i++)
        {
            printf("\nCost %d\n\n",i+1);
            Cost *c=(Cost*)getElementAtPosition(CostList,i);
            PrintCost(c);
        }

    }
    else printf("No cost in the list!");
}

and this is the function that calls the sort function from controller to console:

void AscendingSort(Console* console)
{
    ascending_sort(console->ctrl);
}
3
  • cost not initialized, should be c? Commented Mar 18, 2013 at 12:16
  • Welcome to Stack Overflow. Please read the FAQ before long. Please also note that it is not acceptable to revise a question so that answers that were relevant are rendered irrelevant. You should leave the original code around, pointing out the changes made in the latest version, but you shouldn't completely change the code so that the answers are not relevant. Commented Mar 18, 2013 at 12:42
  • @JonathanLeffler Thanks! I edited again the post. Can you help me now? Commented Mar 18, 2013 at 12:49

3 Answers 3

2

Yes, you're using a pointer to a Cost when you need an actual value. Here's what the innermost scope should be:

const Cost aux = cost[i];
cost[i] = cost[j];
cost[j] = aux;

Note that I don't see (or understand) the relationship between cost, an array of Cost, and c which is of type DynamicVector * and whose length you use in the looping.

Also, your sorting algorithm isn't very nice; you should just use qsort().

Sign up to request clarification or add additional context in comments.

2 Comments

in the for loop i am using the length of c(which is a DynamicVector* type that contains all the costs I enter: Cost1, Cost2, Cost3 etc.) I don't know whtat's qsort() and I want a Bubble Sort like algorithm. :)
@user1849859: You like slow and steady instead of fast and efficient? Bubble sort is slow, O(N*N), whereas qsort() will be O(N log N). The difference really matters when the arrays are big. Bubble sort will take about 100 times as long to sort 1000 records as qsort() will.
1

You used Cost* instead of Cost for aux.

Comments

0

The revised sorting code is better, but is probably making unwarranted assumptions:

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;
            }
        }
    }
}

If you need to use getElementAtPosition(c, i), you need to use getElementAtPosition(c, j) to get the second value. That much seems non-controversial to me.

Additionally, if you need the function to get the values, you probably need a function to put the values too.

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);
        }
    }
}

Or, maybe:

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);
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.