0

I know it's not efficient. But I don't know why it doesn't work...:(


void sort_pairs(void)

{
    pair sorted_pairs[pair_count];
    int strength[pair_count];
    for (int k = 0; k < pair_count; k++)
    {
        strength[k] = preferences[pairs[k].winner][pairs[k].loser];
    }

    int biggest = 0;
    int loc;


    for (int i = 0; i < pair_count; i++)
    {
        for (int k = i; k < pair_count; k++)
        {
            if (strength[k] > biggest)
            {
                biggest = strength[k];
                loc = k;
            }
        }

        int swap = strength[i];
        strength[i] = biggest;
        strength[loc] = swap;
        sorted_pairs[i] = pairs[loc];
    }

    for (int i = 0; i< pair_count; i++)
    {
        pairs[i] = sorted_pairs[i];
    }
    return;
}
1
  • What sorting algorithm are you using? Watch the walkthrough it really helps a lot.
    – Ojou Nii
    Apr 11 '20 at 18:00
1

For the selection sort, you have to reset biggest (and depending on your choice for the initial value, also loc) at the start of the outer loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .