Results 1 to 4 of 4

Thread: qsort (quick sort) function

  1. #1

    Thread Starter
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787

    qsort (quick sort) function

    I want to use the built-in qsort function (stdlib.h) to sort a two dimensional array say arr[10][2];

    could someone help me write the compare function if arr[m][] goes before arr[n][] if arr[m][0]<arr[n][0]
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Two-dim arrays are tricky there...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    // call qsort assuming arr[] is integer
    qsort(&arr,10,sizeof(arr[0][0])*2, cmp)
    
    int cmp(void *a, void *b){
             return ( (int*)a - (int*)b);
    }

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Expected something along those lines, but I was too tired to think properly about it.

    But since he wants to compare the first element of the subarrays this
    return ( (int*)a - (int*)b);
    should be
    return ( (*(int*)a) - (*(int*)b));
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width