简体   繁体   English

C++ 冒泡排序双向链表

[英]C++ Bubble sorting a Doubly Linked List

I know bubble sort is probably not the fastest way to do this but its acceptable.我知道冒泡排序可能不是最快的方法,但它是可以接受的。 i'm just having trouble with adjusting the algorithm to double link lists from arrays.我只是在将算法调整为数组中的双链接列表时遇到问题。

My double linked lists have a type int and a type string to hold a number and a word.我的双链表有一个 int 类型和一个 string 类型来保存一个数字和一个单词。 My list was sorted with an insertion sort that I wrote to sort alphabetically, now I need to reorder my double linked list numerically, greatest to least.我的列表是用我写的按字母顺序排序的插入排序进行排序的,现在我需要按数字重新排序我的双链表,从最大到最小。

My trouble spot is how to run this loop so that it is properly sorted thoroughly and not just one time.我的麻烦点是如何运行这个循环,以便它被正确地彻底排序,而不仅仅是一次。

here's what i've managed to get down so far:这是我到目前为止设法得到的:

void DblLinkedList::ReorderListNumeric()
{
dummy = new Node();
temphead = head;
temp = head->next;

while(tempTwo->next != NULL)
{
    if(temp->wordCount < tempTwo->wordCount)
    {               
        dummy->word = tempTwo->word;
        dummy->wordCount = tempTwo->wordCount;

        tempTwo->word = temp->word;
        tempTwo->wordCount = temp->wordCount;

        temp->word = dummy->word;
        temp->wordCount = dummy->wordCount;
    }
    temp = tempTwo;
    tempTwo = tempTwo->next;
}
}

My trouble spot is how to run this loop so that it is properly sorted thoroughly and not just one time.我的麻烦点是如何运行这个循环,以便它被正确地彻底排序,而不仅仅是一次。

If you already have a loop that will successfully do one pass and swap is okay, the usual way to do multiple passes relatively efficiently is:如果您已经有一个可以成功执行一次传递并且交换没有问题的循环,那么相对有效地执行多次传递的常用方法是:

set swapped = true
while swapped:
    set swapped = false
    do your one pass, setting swapped to true whenever you swap

This avoids the naive n 2 solution that beginners will invariably start with.这避免了初学者总是会开始使用的天真的 n 2解决方案。

And that's it really.就是这样。

You set swapped to true so that you initially enter the list then set it to false immediately, inside the loop.您将swapped设置为true以便您最初进入列表,然后在循环内立即将其设置为false

Your single pass will set swapped only if a swap takes place.你的单次将设置swapped只有当交换发生。 If no swap takes place in your pass, then the list is sorted and you exit the loop.如果在您的通行证中没有发生交换,则对列表进行排序并退出循环。

If any swap takes place, the swapped flag is set and you will need to run another pass.如果发生任何交换,则设置swapped标志,您将需要运行另一遍。 This is because the swap could be at the end of the list and invalidate the order earlier on, such as:这是因为交换可能位于列表的末尾并使之前的订单无效,例如:

Initial: 1   2   3   4   6   7   5
  Pass1: 1   2   3   4   6   5<=>7 (swap)
  Pass2: 1   2   3   4   5<=>6   7 (swap)
  Pass3: 1   2   3   4   5   6   7 (no swap, so exit loop)

So, assuming your code is correct, start with something like:因此,假设您的代码是正确的,请从以下内容开始:

void DblLinkedList::ReorderListNumeric() {
    Node *ptr, *dummy = new Node();

    // Zero or one element, no sort required.

    if (head == NULL) return;
    if (head->next == NULL) return;

    // Force initial entry.

    int swapped = 1;
    while (swapped) {
        // Flag as last time, then do one pass.

        swapped = 0;

        ptr = head;
        while (ptr->next != NULL) {
            if (ptr->wordCount < ptr->next->wordCount) {
                // Swapping, need another pass.

                swapped = 1;

                dummy->word = ptr->word;
                ptr->word = ptr->next->word;
                ptr->next->word = dummy->word;

                dummy->wordCount = ptr->wordCount;
                ptr->wordCount = ptr->next->wordCount;
                ptr->next->wordCount = dummy->wordCount;
            }
            ptr = ptr->next;
        }
    }
}

Use 2 loops to sort the list thoroughly ( I am not considering the efficiency here as thats not important to you at the moment).使用 2 个循环对列表进行彻底排序(我不考虑这里的效率,因为目前这对您来说并不重要)。 So iterate the list with 2 pointers as you would do with arrays -所以像处理数组一样用 2 个指针迭代列表 -

void DblLinkedList::ReorderListNumeric()
{
    NODE* temphead = NULL; // assuming your list is made of NODEs
    NODE* temp = NULL;

    for(temphead = head; temphead && temphead->next != NULL; ++ temphead)
    {
        for(temp = temphead->next; temp && temp->next != NULL; ++temp)
        {
            if(temphead->wordCount < temp->wordCount)
            {
                std::string dummyWord = temp->word;
                int dummyCount = temp->wordCount;

                temp->word = temphead->word;
                temp->wordCount = temphead->wordCount;

                temphead->word = dummyWord;
                temphead->wordCount = dummyCount;
            }
        }
    }
}

BTW, Why do want to create a dummy node to swap, when all you want is to swap the values.顺便说一句,当您只想交换值时,为什么要创建一个虚拟节点来交换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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