0

I have an array of the following struct

typedef struct monom {
    int coefficient; 
    int power;
}MONOM;

I have an array of monoms and I want to send it to a merge-sort function, when I do it, using the following call,

mergeSort(&polynomial, logSize);

only the first monom of the array of monoms is sent to the merge-sort function.

When I debug the function I see the full array on the line before the call to mergeSort but when I continue into mergeSort, only the first element is sent.

These are my mergeSort and merge, I'm completely clueless:

void mergeSort(MONOM** polynomial, int size)
{
    MONOM** res;
    int i;

    if (size < 2)
        return;

    else
    {
        mergeSort(*polynomial, size/2); // merge first half of array
        mergeSort(*polynomial+(size/2),size-(size/2)); // merge second half of array

        // allocate result array
        res = (MONOM**)malloc(size*sizeof(MONOM*));

        // merge both sorted halfs of the array into 'res'
        merge(*polynomial,size/2,*polynomial+(size/2),size-(size/2),res);

        // copy 'res' to 'arr'
        for (i = 0; i < size; i++)
            polynomial[i] = res[i];

        // release unused memory
        free(res);
    }
}

void merge(MONOM** poly1, int n1, MONOM** poly2, int n2, MONOM** res)
{
    int i1 = 0, i2 = 0;
    int resIndex = 0;

    while (i1 < n1 && i2 < n2)
    {
        if (poly1[i1]->power < poly2[i2]->power)
        {
            res[resIndex] = poly2[i2];
            i2++;
        }

        else if (poly1[i1]->power > poly2[i2]->power)
        {
            res[resIndex] = poly1[i1];
            i1++;
        }

        else
        {
            res[resIndex]->power = poly1[i1]->power;
            res[resIndex]->coefficient = poly1[i1]->coefficient + poly2[i2]->coefficient;
            i1++;
            i2++;
        }

        resIndex++;
    }

    // fill 'res' array when one of the arrays is finished
    while (i1 < n1)
    {
        res[resIndex] = poly1[i1];
        i1++;
        resIndex++;
    }

    while (i2 < n2)
    {
        res[resIndex] = poly2[i2];
        i2++;
        resIndex++;
    }
}
5
  • The first argument of mergeSort() is of type MONOM **. You're calling it recursively as mergeSort(*polynomial, size/2) - i. e. as its first argument, you pass in a MONOM *. What do you expect? Commented Jul 18, 2013 at 17:30
  • By the way, don't roll your own sorting algorithm, qsort() from <stdlib.h> works well. Commented Jul 18, 2013 at 17:30
  • 1
    Don't believe everything a debugger says. Commented Jul 18, 2013 at 17:30
  • @H2CO3 it's not the usual merge-sort, it also combines elements with the same power so I don't think any lib function will work Commented Jul 18, 2013 at 17:42
  • You might want to look at this stackoverflow.com/questions/6645161/… Commented Jul 18, 2013 at 17:50

1 Answer 1

1

Try this,

mergeSort(polynomial, logSize); (Given polynomial is MONOM* or MONOM[] type)


and also

void mergeSort(MONOM* polynomial, int size)

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

9 Comments

but I need polynomial to change, I must call it with an & don't I?
Not for an array, for an array by default it will change(without using & operator). Using & operator refers to the first element of the array(hence your problem)
You have to make many changes for your program. Eg, In some places you have dereferenced the pointer twice
@TheJoker No, the & operator refers to the entire array, and it yields a pointer-to-array type (of which the value can be the same as the pointer to the first element of the array, which the array decays into).
You are doing that by default. An array name always decays into pointer to the first element of the array.
|

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.