简体   繁体   中英

qsort struct not doing as supposed to

Hi so I'm trying to sort my struct with qsort and it sort of works but not really... Can anyone tell me what's happening and why it goes wrong? :)

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

int el_cmp(const void *ep1, const void *ep2);

typedef struct kort
{
    int kuloer;
    int vaerdi;
} kort;

int main(void){
    int i; 
    int k[] = {3, 4, 5, 6};
    int v[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    
    kort kort[52];

    for (i = 0; i < 52; i++){

        kort[i].kuloer = k[i % 4];
        kort[i].vaerdi = v[i % 13];

        
    }
    printf("\n");

    qsort(kort, 52, sizeof(int), el_cmp);
 
    for (i = 0; i < 52; i++){

        printf("Kort%d: %d %d\n", i + 1, kort[i].kuloer, kort[i].vaerdi);
    }
     

    return(0);
}

int el_cmp(const void *ep1, const void *ep2){
    int *tp1 = (int*) ep1,
        *tp2 = (int*) ep2;

    if (*tp1 < *tp2){
        return -1;
    } else if (*tp1 > *tp2){
        return 1;
    } else return 0;
}

the result is this:

Kort1: 2 2

Kort2: 3 3

Kort3: 3 3

Kort4: 3 3

... Kort5: 3 3, Kort6: 3 4, Kort7: 4 4, Kort8: 4 4, Kort9: 4 4, Kort10: 4 4, Kort11: 5 5, Kort12: 5 5, Kort13: 5 5, Kort14: 5 5, Kort15: 6 6, Kort16: 6 6 Kort17: 6 6, Kort18: 6 6, Kort19: 7 7, Kort20: 8 8, Kort21: 9 9, Kort22: 10 10, Kort23: 11 11, Kort24: 12 12, Kort25: 13 13, Kort26: 14 14,

Kort27: 5 2

Kort28: 6 3

Kort29: 3 4

... Kort30: 4 5, Kort31: 5 6, Kort32: 6 7, Kort33: 3 8, Kort34: 4 9, Kort35: 5 10, Kort36: 6 11, Kort37: 3 12, Kort38: 4 13, Kort39: 5 14, Kort40: 6 2, Kort41: 3 3, Kort42: 4 4, Kort43: 5 5, Kort44: 6 6, Kort45: 3 7, Kort46: 4 8, Kort47: 5 9, Kort48: 6 10, Kort49: 3 11, Kort50: 4 12, Kort51: 5 13, Kort52: 6 14,

but I'm expecting this:

Kort1: 3 2

Kort2: 3 3

Kort3: 3 4

Kort4: 3 5

... Kort5: 3 6, Kort6: 3 7, Kort7: 3 8, Kort8: 3 9, Kort9: 3 10, Kort10: 3 11, Kort11: 3 12, Kort12: 3 13, Kort13: 3 14,

Kort14: 4 2

Kort15: 4 3

... Kort16: 4 4, Kort17: 4 5, Kort18: 4 6, Kort19: 4 7, Kort20: 4 8, Kort21: 4 9, Kort22: 4 10, Kort23: 4 11, Kort24: 4 12, Kort25: 4 13, Kort26: 4 14,

... Kort27: 5 2, Kort28: 5 3, Kort29: 5 4, Kort30: 5 5, Kort31: 5 6, Kort32: 5 7, Kort33: 5 8, Kort34: 5 9, Kort35: 5 10, Kort36: 5 11, Kort37: 5 12, Kort38: 5 13, Kort39: 5 14,

... Kort40: 6 2, Kort41: 6 3, Kort42: 6 4, Kort43: 6 5, Kort44: 6 6, Kort45: 6 7, Kort46: 6 8, Kort47: 6 9, Kort48: 6 10, Kort49: 6 11, Kort50: 6 12, Kort51: 6 13, Kort52: 6 14,

For starters it is a bad idea to redeclare the name kort

kort kort[52];

Nevertheless this call of qsort

qsort(kort, 52, sizeof(int), el_cmp);

is incorrect. You have an array of structures of the type struct kort not of the type int . The call of qsort can look the following way

qsort(kort, sizeof( kort ) / sizeof( *kort ), sizeof( *kort ), el_cmp);

The comparison function can look the following way

int el_cmp( const void *ep1, const void *ep2 )
{
    const struct kort *a = ep1;
    const struct kort *b = ep2;

    int result = ( b->kuloer < a->kuloer ) - ( a->kuloer < b->kuloer );

    if ( result == 0 )
    {
        result = ( b->vaerdi < a->vaerdi ) - ( a->vaerdi < b->vaerdi );
    }

    return result;
}

Here is your updated program.

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

typedef struct kort
{
    int kuloer;
    int vaerdi;
} kort;

int el_cmp( const void *ep1, const void *ep2 )
{
    const struct kort *a = ep1;
    const struct kort *b = ep2;

    int result = ( b->kuloer < a->kuloer ) - ( a->kuloer < b->kuloer );

    if (result == 0)
    {
        result = ( b->vaerdi < a->vaerdi ) - ( a->vaerdi < b->vaerdi );
    }

    return result;
}

int main( void )
{
    int k[] = { 3, 4, 5, 6 };
    int v[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };

    const size_t k_size = sizeof( k ) / sizeof( *k );
    const size_t v_size = sizeof( v ) / sizeof( *v );

    enum { N = 52 };

    kort kort[N];

    for ( size_t i = 0; i < N; i++ ) 
    {

        kort[i].kuloer = k[i % k_size];
        kort[i].vaerdi = v[i % v_size];
    }

    qsort( kort, N, sizeof( *kort ), el_cmp );

    for ( size_t i = 0; i < N; i++ ) 
    {
        printf( "Kort%zu: %d %d\n", i + 1, kort[i].kuloer, kort[i].vaerdi );
    }
    putchar( '\n' );
}

The program output is the same as you are expecting

Kort1: 3 2
Kort2: 3 3
Kort3: 3 4
Kort4: 3 5
Kort5: 3 6
Kort6: 3 7
Kort7: 3 8
Kort8: 3 9
Kort9: 3 10
Kort10: 3 11
Kort11: 3 12
Kort12: 3 13
Kort13: 3 14
Kort14: 4 2
Kort15: 4 3
Kort16: 4 4
Kort17: 4 5
Kort18: 4 6
Kort19: 4 7
Kort20: 4 8
Kort21: 4 9
Kort22: 4 10
Kort23: 4 11
Kort24: 4 12
Kort25: 4 13
Kort26: 4 14
//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM