Home > Enterprise >  Bubble sort not working when I implement it in a function
Bubble sort not working when I implement it in a function

Time:12-30

I have this code to order a group of teams based on their scores, just like a soccer ranking and the code works fine when implemented like this (btw I defined "NEQS" to 18):

int melhor_class(t_equipa *eqA, t_equipa *eqB)
{
    if(eqA->pontos > eqB->pontos){
        return 1;
    } else if(eqA->pontos < eqB->pontos){
        return 0;
    } else if(eqA->golosM > eqB->golosM){
        return 1;
    } else if(eqA->golosM < eqB->golosM){
        return 0;
    } else if(eqA->golosS < eqB->golosS){
        return 1;
    } else if(eqA->golosS > eqB->golosS){
        return 0;
    } else {
        return 1;
    }
}

void ordenar_equipas(t_equipa *e)
{
    for(int i = 0; i < NEQS - 1; i  )
    {
        for(int j = 0; j < NEQS - i - 1; j  )
        {
            if (melhor_class(&e[j],&e[j 1]) == 0)
            {
                //swaping part
                t_equipa temp = e[j];
                e[j] = e[j 1];
                e[j 1] = temp;
                
            }
        }
    }
}

And the output works fine, its sorted out:

                         P  V  E  D  M  S
Gil Vicente             33  0  0  0  18  7
Benfica                 32  0  0  0  10  10
Sporting                31  0  0  0  10  7
Porto                   24  0  0  0  20  8
Arouca                  0  0  0  0  0  0
Belenenses              0  0  0  0  0  0
Boavista                0  0  0  0  0  0
Braga                   0  0  0  0  0  0
Estoril                 0  0  0  0  0  0
Famalicao               0  0  0  0  0  0
Maritimo                0  0  0  0  0  0
Moreirense              0  0  0  0  0  0
Pacos Ferreira          0  0  0  0  0  0
Portimonense            0  0  0  0  0  0
Santa Clara             0  0  0  0  0  0
Tondela                 0  0  0  0  0  0
Vitoria                 0  0  0  0  0  0
Vizela                  0  0  0  0  0  0

But when I put the swaping part of the code in an function it just doesn't work:

void trocar_equipas(t_equipa *e, int p1, int p2)
{
    t_equipa temp = e[p1];
    e[p1] = e[p2];
    e[p2] = temp;

}

void ordenar_equipas(t_equipa *e)
{
    for(int i = 0; i < NEQS - 1; i  )
    {
        for(int j = 0; j < NEQS - i - 1; j  )
        {
            if (melhor_class(&e[j],&e[j 1]) == 0)
            {
                trocar_equipas(&e,j,j 1);
            }
        }
    }
}

Output:

                        P  V  E  D  M  S
Arouca                  0  0  0  0  0  0
Belenenses              0  0  0  0  0  0
Benfica                 32  0  0  0  10  10
Boavista                0  0  0  0  0  0
Braga                   0  0  0  0  0  0
Estoril                 0  0  0  0  0  0
Famalicao               0  0  0  0  0  0
Gil Vicente             33  0  0  0  18  7
Maritimo                0  0  0  0  0  0
Moreirense              0  0  0  0  0  0
Pacos Ferreira          0  0  0  0  0  0
Porto                   24  0  0  0  20  8
Portimonense            0  0  0  0  0  0
Santa Clara             0  0  0  0  0  0
Sporting                31  0  0  0  10  7
Tondela                 0  0  0  0  0  0
Vitoria                 0  0  0  0  0  0
Vizela                  0  0  0  0  0  0

I really need to put that swaping part in another function! I appreciate any type of help! Thanks

CodePudding user response:

The function trocar_equipas receives a pointer as an argument, so you can just pass it like this:

void trocar_equipas(t_equipa *e, int p1, int p2)
{
    t_equipa temp = e[p1];
    e[p1] = e[p2];
    e[p2] = temp;

}

void ordenar_equipas(t_equipa *e)
{
    for(int i = 0; i < NEQS - 1; i  )
    {
        for(int j = 0; j < NEQS - i - 1; j  )
        {
            if (melhor_class(&e[j],&e[j 1]) == 0)
            {
                trocar_equipas(e,j,j 1);
            }
        }
    }
}

CodePudding user response:

The first argument expression of this call

trocar_equipas(&e,j,j 1);

is incorrect.

It has the type t_equipa ** while the function trocar_equipas expects an argument of the type t_equipa *.

void trocar_equipas(t_equipa *e, int p1, int p2)
{
    t_equipa temp = e[p1];
    e[p1] = e[p2];
    e[p2] = temp;

}

So call the function like

trocar_equipas(e,j,j 1);

Another approach is to declare and define the function trocar_equipas the following way

void trocar_equipas( t_equipa *e1, t_equipa *e2 )
{
    t_equipa temp = *e1;
    *e1 = *e2;
    *e2 = temp;
}

and call it like

trocar_equipas( &e[j], &e[j 1] );

or

trocar_equipas( e   j, e   j   1 );
  • Related