an actual question (sorting arrays)

This is a discussion on an actual question (sorting arrays) within the C Programming forums, part of the General Programming Boards category; Is there an easy way to sort a multi-dimensional array using qsort?...

  1. #1
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    an actual question (sorting arrays)

    Is there an easy way to sort a multi-dimensional array using qsort?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Depends what you call easy. The only really tricky bit to qsort is writing the comparision function.

    The only downside to sorting multi dimensional arrays is the size of elements which qsort must swap. This example has to swap 20 bytes to exchange strings, where it would be more efficient if pointers were used.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUM_ENTRIES(c)  (sizeof(c)/sizeof(c[0]))
    
    /* you would really want to use char*'s here */
    /* its much easier to swap a pointer than an array of 20 chars */
    char test[][20] = {
        "hello",
        "world",
        "how",
        "are",
        "you"
    };
    
    /* define a typedef for the minor array subscripts */
    /* better than writing const char (*pa)[20] = a; */
    typedef char foo[20];
    
    int compare ( const void *a, const void *b ) {
        const foo *pa = a;
        const foo *pb = b;
        return strcmp( *pa, *pb );
    }
    
    int main ( ) {
        int i, num = NUM_ENTRIES(test);
        printf( "     Before\n" );
        for ( i = 0 ; i < num ; i++ ) printf( "%s\n", test[i] );
        qsort( test, num, sizeof(test[0]), compare );
        printf( "\n     After\n" );
        for ( i = 0 ; i < num ; i++ ) printf( "%s\n", test[i] );
        return 0;
    }
    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
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    MSVC++

    Code:
    const foo *pa = (const char(*)[20])a;
    const foo *pb = (const char(*)[20])b;
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well that is certainly better than the alternative--writing my own qsort.

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I have another qsort solution somewheres...
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  2. Replies: 11
    Last Post: 11-26-2004, 07:13 PM
  3. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM
  4. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM
  5. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 11:35 AM

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