Sorting 2Dimensional Array....

This is a discussion on Sorting 2Dimensional Array.... within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to sort a 2D array of type Struct CvPoint using qsort(). Structure details: Code: typedef ...

1. Sorting 2Dimensional Array....

Hi all,

I am trying to sort a 2D array of type Struct CvPoint using qsort().
Structure details:

Code:
```typedef struct CvPoint
{
int x; /* x-coordinate, usually zero-based */
int y; /* y-coordinate, usually zero-based */
}
CvPoint;```

Array details :

edges[i][j]
i -> 0 to no. of edges;
j -> 2 :: 0 START & 1 END;

Where START and END are the CvPoints and their structure is shown above.

qsort is working properly when i am using the following code lines in red.

Code:
```#define START 0
#define END 1

qsort(edges, edg, sizeof(CvPoint**), compare);

int compare(const void* a, const void* b)
{
CvPoint *arg1, *arg2;
arg1 = ((CvPoint*)a);
arg2 = ((CvPoint*)b);

if( (*((int*)arg1[START].x)) > (*((int*)arg2[START].x)) )
return 1;
else if(( *((int*)arg1[START].x)) == (*((int*)arg2[START].x)) )
return 0;
else
return -1;
}```

But is failling when i try the code in green lines:

Code:
```#define START 0
#define END 1

qsort(edges, edg, sizeof(CvPoint**), compare);

int compare(const void* a, const void* b)
{
CvPoint *arg1, *arg2;
arg1 = ((CvPoint*)a);
arg2 = ((CvPoint*)b);

if( (*((int*)arg1[START].y)) > (*((int*)arg2[START].y)) )
return 1;
else if(( *((int*)arg1[START].y)) == (*((int*)arg2[START].y)) )
return 0;
else
return -1;
}```

Why it so happening??
I am able to structure element 'x' of the struct CvPoint. But, i am unable to access the structure element 'y' of the CvPoint.

Thanks and Rargards,

(anjaneya4u at gmail dot com)

T array[SIZE];

The call to qsort is
qsort( array, SIZE, sizeof(array[0]), compareFunction );

Now the compare function receives two T* pointers, initially cast to void*,
So typically, we have
Code:
```int compareFunction ( const void *a, const void *b ) {
const T* pa = a;
const T* pb = b;
// now compare pa->member with pb->member (for a struct)
// or *pa and *pb for a scalar value
}```

3. Hi,

But, i want to sort out the 2 Dimensional Array not the 1 Dimensional array.
Ofcourse i did it through the code written by me.

I want to know what is the behaviour of this function, Why it is so giving unregular results???

Regards,

4. Doesn't matter, arrays are contiguous.
Code:
```int test[][3] = {{3,5,1}, {5,2,3}, {19,14,5}};

qsort(test, sizeof(test) / sizeof(test[0][0]), sizeof(test[0][0]), myCompare);```
Can also be written as,
Code:
```int test[] = {3, 5, 1, 5, 2, 3, 19, 14, 5};

qsort(test, sizeof(test) / sizeof(test[0]), sizeof(test[0]), myCompare);```
>I want to know what is the behaviour of this function, Why it is so giving unregular results???
You mean qsort()? It's not. Do a man qsort if you want to know how it works.

5. Originally Posted by zacs7
Doesn't matter, arrays are contiguous.
Code:
```int test[][3] = {{3,5,1}, {5,2,3}, {19,14,5}};

qsort(test, sizeof(test) / sizeof(test[0][0]), sizeof(test[0][0]), myCompare);```
Can also be written as,
Code:
```int test[] = {3, 5, 1, 5, 2, 3, 19, 14, 5};

qsort(test, sizeof(test) / sizeof(test[0]), sizeof(test[0]), myCompare);```
>I want to know what is the behaviour of this function, Why it is so giving unregular results???
You mean qsort()? It's not. Do a man qsort if you want to know how it works.
Actually i think the results of that wouldnt be what he wants.

Lets say the data looks like this:

Code:
```int test[][3] = {
{8, 2, 5},
{0, 7, 2},
{99, 5, 33}
};```
I think the results would look like this:

Code:
```int test[][3] = {
{0, 2, 2},
{5, 5, 7},
{8, 33, 99}
};```
But he may want the results to look like this:
Code:
```int test[][3] = {
{2, 5, 8},
{0, 2, 7},
{5, 33, 99}
};```

6. Either way he has the basis to do both, by himself.

7. Exactly 39ster.

I want similar sorting result.
I tried it. But, i couldn't. Thats why i have changed my code.

8. Code:
```qsort(test[0], sizeof(test[0]) / sizeof(test[0][0]), sizeof(test[0][0]), myCompare);
qsort(test[1], sizeof(test[1]) / sizeof(test[1][0]), sizeof(test[1][0]), myCompare);   /* 1s changed everywhere to show meaning */

/* ... */```
And so on and so forth maybe?

Perhaps a loop?
Code:
```for(i = 0; i < (sizeof(test) / sizeof(*test)); i++)
{
qsort(test[i]... blah blah blah
}```

9. In other words: you can look at the problem as sorting a bunch of 1D arrays. Simply step through the elements in the array to get 1D arrays that you can sort . . . .