Thread: qsort() won't work properly..

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    qsort() won't work properly..

    Hi
    i am facing some problem with qsort() function it work well if the last element of array is larger then the 2nd last element. But in case if last element of array is smaller then the 2nd last it will sort the whole array and remains the last as it is. For example
    Code:
    int group_id_local[max_j]={2,1,4,5};// it work fine, output should be {1,2,4,5} 
    //but if i have this one 
    int group_id_local[max_j]={2,1,4,3};// output should be {1,2,4,3}
    /* COMPARE FUNCTION FOR USING QSORT()*/
    int cmpfunc (const void* a, const void* b)
    {
    if (*(int *)a < *(int *)b) return -1;
    if (*(int *)a > *(int *)b) return 1;
    return 0;
    }
    
    qsort(group_id_local,max_j, sizeof(int), cmpfunc);
    int j;
    printf ("sorted Array:\n");
    for (j=0; j<=max_j; j++){
    printf ("%d\n ",group_id_local[j]);
    }
    Plz help me why it will not sort the last element?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (j=0; j<=max_j; j++)
    Perhaps it's because you have an array overrun in your printing.

    So whatever you think is the last element is actually garbage.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    Code:
    for (j=0; j<=max_j; j++){
    Code:
    printf ("%d\n ",group_id_local[j]);
    }
    Code:
    here max_j=3
    and we have 4 elements thats why i use j<=max_j.
    It is not overrun. This thing make me crazy. I spend alot of time to figure out whats going on but still the same issue.

    Quote Originally Posted by Salem View Post
    > for (j=0; j<=max_j; j++)
    Perhaps it's because you have an array overrun in your printing.

    So whatever you think is the last element is actually garbage.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    I get my problem where i am calling qsort()
    Code:
    qsort(group_id_local,max_j, sizeof(int), cmpfunc);
    // I was passing the wrong count, my count should be max_j+1 
    qsort(group_id_local,max_j+1, sizeof(int), cmpfunc)
    Thanks

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by gevni View Post
    Code:
    int group_id_local[max_j]={2,1,4,5};// it work fine, output should be {1,2,4,5} 
    //but if i have this one 
    int group_id_local[max_j]={2,1,4,3};// output should be {1,2,4,3}

    Quote Originally Posted by gevni View Post
    Code:
    for (j=0; j<=max_j; j++)
    {
         printf ("%d\n ",group_id_local[j]);
    }
    here max_j=3

    and we have 4 elements thats why i use j<=max_j.
    Quote Originally Posted by gevni View Post
    Code:
    qsort(group_id_local,max_j, sizeof(int), cmpfunc);
    // I was passing the wrong count, my count should be max_j+1 
    qsort(group_id_local,max_j+1, sizeof(int), cmpfunc)

    Something is wrong. If "max_j" really is 3 then you should get a warning when you initialize your array and you would still have a buffer overrun. Or do you decrease "max_j" after initializing (which would be rather strange)?

    Bye, Andreas

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Funny, because it works for me when I don't play silly games with <= and +/- 1 trying to count the number of elements in the array.

    Code:
    int group_id_local[max_j]={2,1,4,5};// it work fine, output should be {1,2,4,5}
    This means max_j is 4
    Nothing more, nothing less - if you're not using 4, you're doing it wrong.

    Then
    qsort(group_id_local,max_j, sizeof(int), cmpfunc);

    And
    for (j=0; j<max_j; j++)

    follow on naturally from that.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to get a few functions to work properly...
    By lemonwaffles in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2009, 11:00 PM
  2. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM
  3. GetAsyncKeyState() won't work properly!
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-30-2002, 03:03 PM
  4. Qsort doesn't work for floats!
    By Sebastiani in forum C Programming
    Replies: 2
    Last Post: 09-14-2001, 12:34 PM