0

I'm trying to sort multidimensional array with qsort, but the result is nonsense..

My code (excerpt):

#include <stdio.h>
#include <stdlib.h>

int srovnejVelikost(const void *p1, const void *p2) {
    const long int (*a)[5] = p1;
    const long int (*b)[5] = p2;
    return ((*a)[0] + (*a)[1] - (*b)[0] - (*b)[1]);
}

long int nadrze[200000][5];

[values added into the array here]

qsort (nadrze, 200000, sizeof(nadrze[0]), srovnejVelikost);

It should sort nadrze[] according to the result of (nadrze[a][0] + nadrze[a][1] - nadrze[b][0] - nadrze[b][1]).... all 5 elements moved

Thanks for your help. It is most appreciated.

5
  • "the result is nonsense" First specify how a multidimensional array is considered "sorted". For me, without any information, that has no sense at all. Commented Nov 10, 2013 at 18:33
  • Also consider to translate the code to english. That helps a lot to understand your code. Commented Nov 10, 2013 at 18:34
  • The language is not the problem, translating wouldn't be of any help in this case... For example: nadrze[0] = {-10,20,0,0,0}, nadrze[1] = {5,40,0,0,0}; The sorting should be done by comparing (nadrze[0][0] + nadrze[0][1]) and (nadrze[1][0] + nadrze[1][1]). The latter is bigger (45 against 10), so nadrze[1] should be after nadrze[0] in the sorted array. The problem is that when printing the values after the sort, I get huge numbers which make no sense for me. (there should be the same values as before the sort, just sorted) Commented Nov 10, 2013 at 18:44
  • You are mixing types here. I'd suggest using an array of struct instead. That will clarify a lot to you and us. Commented Nov 10, 2013 at 19:00
  • You might try heeding the warning from your compiler about losing integer precision from long int to int in your return value of your comparator. Just saying. Also, your logic in your comparator can easily cause overflow or underflow with proper(term used loosely) values. That said, try it with a smaller array (say 20 elements) and post the results in your question, along with the original array values and order. Commented Nov 10, 2013 at 19:05

2 Answers 2

1

If your sort criteria is truly nadrze[a][0] + nadrze[a][1] - nadrze[b][0] - nadrze[b], I find the following example to represent it:

#include <stdio.h>
#include <stdlib.h>

#define max_i 5
#define max_j 5

int srovnejVelikost(const void *p1, const void *p2) {
    const long int *a = p1;
    const long int *b = p2;
    return ((a[0] + a[1]) - (b[0] + b[1]));
}

int main(int argc, char **argv)
{
    int i = 0;
    int j = 0;
    long int nadrze[max_i][max_j];
    long int d = 0;

    printf("Before\n");
    for (i = 0; i < max_i; ++i) {
            d = (nadrze[i][0] + nadrze[i][1]);
            printf("i: %d %d\n",i, d);
    }

    qsort(nadrze, max_i, sizeof(long int)*max_j, srovnejVelikost);

    printf("After\n");
    for (i = 0; i < max_i; ++i) {
            d = (nadrze[i][0] + nadrze[i][1]);
            printf("i: %d %d\n",i, d);
    }


    return(EXIT_SUCCESS);

} 

You can expand out max_i and max_j as your case needs. I just used defines as I was testing.

Also note, I coded it as (a[0] + a[1]) - (b[0] + b[1]) as this is equivalent to a[0] +a[1] - b[0] - b[1] and it makes more sense as I read it for a comparator.

You need to pass the whole row to qsort, which I do by saying the width is `sizeof(ling int)*max_j, which is 5 in your case.

Does this make sense?

(The qsort comparison function doesn't care if you pass it one int or an array, it is up to you on how you deal with it. So watch our you don't access beyond what your meant to.)

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

Comments

0
int arr[10][100];
for(i=0;i<10;i++){
    qsort(arr[i],------,-----,----);
}

It is multidimensional array you can sort one at a time, qsort cannot sort all at the same time.

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.