2-dim Array sortieren



  • Hallo,

    wie sortiert man (am schnellsten) einen 2-dim Array?
    Habe einen Array mit Zahlen, bei welchem die min-Werte
    von Interesse sind!

    Gruß
    daMike



  • willst du wirklich sortieren, oder suchen nach dem kleinsten Wert???



  • Ein kleines Konsolenbeispiel:

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <conio.h>
    using namespace std;
    
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
    int main()
    {
        const int I = 10;
        const int J = 6;
        srand( static_cast<unsigned>(time(0)) ); 
    
        int Array[I][J];
        for (int i=0; i<I; ++i)
        {
            for (int j=0; j<J; ++j)
            {
                Array[i][j] = rand()%49+1;           
            }
        }
    
        cout << "unsortiert:" << endl;
        for (int i=0; i<I; ++i)
        {
            for (int j=0; j<J; ++j)
            {
                cout << Array[i][j] << '\t';           
            }
            cout << endl;
        }
    
        for (int i=0; i<I; ++i)
        {
                qsort(Array[i],J,sizeof(int),compare);
        }
    
        cout << endl << "sortiert:" << endl;
        for (int i=0; i<I; ++i)
        {
            for (int j=0; j<J; ++j)
            {
                cout << Array[i][j] << '\t';           
            }
            cout << endl;
        }
    
        cout << endl << "Minima:" << endl;
        for (int i=0; i<I; ++i)
        {
                cout << Array[i][0] << '\t';           
        }    
    
        getch();
    }
    


  • Wenn Du das ganze Array ohne Rücksicht auf Verluste sortieren willst:

    qsort(Array,I*J,sizeof(int),compare);
    


  • Was gibt man da bei 'compare' an?



  • The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:

    compare( (void 😉 elem1, (void 😉 elem2 );

    The routine must compare the elements, then return one of the following values:

    Return Value Description
    < 0 elem1 less than elem2
    0 elem1 equivalent to elem2
    > 0 elem1 greater than elem2

    The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than„ and “less than„ in the comparison function.

    Example

    /* QSORT.C: This program reads the command-line
    *
    parameters and uses qsort to sort them. It
    * then displays the sorted arguments.
    */

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>

    int compare( const void *arg1, const void *arg2 );

    void main( int argc, char **argv )
    {
    int i;
    /* Eliminate argv[0] from sort: */
    argv++;
    argc--;

    /* Sort remaining args using Quicksort algorithm: */
    qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

    /* Output sorted list: */
    for( i = 0; i < argc; ++i )
    printf( "%s ", argv[i] );
    printf( "\n" );
    }

    int compare( const void *arg1, const void *arg2 )
    {
    /* Compare all of both strings: /
    return _stricmp( * ( char
    * ) arg1, * ( char** ) arg2 );
    }

    kleiner Auszug aus der msdn



  • z.B.: (siehe oben)

    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    


  • qsort is aus dem C-Teil

    Nimm sort aus der STL, dann kannst dir für Standardtypen auch die Funktion sparen.
    #include <algorithm> nicht vergessen!



  • Ja, richtig. sort aus der STL ist heute der bessere Tipp (verwendet ja auch den qsort algo). Sorry für das alte Gerümpel, lag so rum. 🙄


Anmelden zum Antworten