C++ bubble sort on doubly linked list

Viewed 154

I saw the bubble sort algorithm on https://www.geeksforgeeks.org/bubble-sort-on-doubly-linked-list/, and I want the sorting function to perform ascending order and descending order like a switch, it works fine on ascending case, but it doesn't work when it's descending, why? Here is the Node and sorting function:
The Node has dynamic string array as data, so I want it to sort only base on a certain column, the index of column is index in bubbleSort.

struct EntryNode {
    string *entry;

    EntryNode *prev;
    EntryNode *next;
};
void bubbleSort(EntryNode *start, int index, enum order order) {
    int swapped, i;
    EntryNode *ptr1;
    EntryNode *lptr = NULL;

    /* Checking for empty list */
    if (start == NULL) return;

    if (order == ASCENDING) {
        do
        {
            swapped = 0;
            ptr1 = start;

            while (ptr1->next != lptr)
            {
                if (ptr1->entry[index].compare(ptr1->next->entry[index])>0)
                {
                    ll_swapData(ptr1, ptr1->next);
                    swapped = 1;
                }
                ptr1 = ptr1->next;
            }
            lptr = ptr1;
        }
        while (swapped);
    }

    if (order == DESCENDING) {
        do
         {
             swapped = 0;
             ptr1 = start;

             while (ptr1->next != lptr)
             {
                 if (ptr1->entry[index].compare(ptr1->next->entry[index])<0)
                 {
                     ll_swapData(ptr1, ptr1->next);
                     swapped = 1;
                 }
                 ptr1 = ptr1->next;
             }
             lptr = ptr1;
         }
         while (swapped);
    }
}
0 Answers
Related