lundi 29 novembre 2021

Bubble sort in double linked list

void sortTrain(TrainCar* head, bool ascending)
{
    TrainCar* current = head;
    int count = 1;
    int size = (getlength(head));
    if (ascending == 1)
    {
        for(int i = 0; i < size-1; i++)
        {
            while(current->next)
            {
                if((current->load) > ((current->next)->load))
                {
                    swapCar(head,count,count+1);
                }
                count++;
                current = current->next;
            }
        }
    }

    if (ascending == 0)
    {
        for(int i = 0; i < size-1; i++)
        {
            while(current->next)
            {
                if((current->load) < ((current->next)->load))
                {
                    swapCar(head,count,count+1);
                }
                count++;
                current = current->next;
            }
        }
    }
}

Anyone helps me to fix the problem? I don't know how to improve it. Or any other code can do the same result? Ascending when bool ascending is true, otherwise, do descending.

Aucun commentaire:

Enregistrer un commentaire