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. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    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.

    Can any one please help me in this regard.

    Thanks and Rargards,

    Anjaneya Prasad .N
    (anjaneya4u at gmail dot com)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    You start with this
    T array[SIZE];
    Where T is your type.

    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
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    Hi,

    Thanks for your reply SALEM.
    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,
    Anjaneya Prasad .N

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.
    Last edited by zacs7; 06-02-2008 at 02:13 AM.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by zacs7 View Post
    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}
    };
    Last edited by 39ster; 06-02-2008 at 04:32 AM.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Either way he has the basis to do both, by himself.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    3
    Exactly 39ster.

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

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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
    }
    Last edited by zacs7; 06-02-2008 at 05:25 AM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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 . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 04:30 AM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Sorting an array
    By OnionKnight in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 04:31 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. sorting array efficiently...can't find the error
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 03:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21