简体   繁体   English

链表选择排序算法中的逻辑错误

[英]Logic error in a linked list selection sort algorithm

I have gotten it to consistently work with 3 nodes, but when I raise it to 10 nodes, I get a stack that is partially sorted. 我已经使它能够与3个节点一致地工作,但是当我将其提高到10个节点时,我得到了部分排序的堆栈。 The linked list class that this is attatched to definitely works properly, and I have gotten bubbleSort working properly so far. 该链接到的链表类肯定可以正常工作,到目前为止,我已经使bubbleSort可以正常工作。 first points to the first node in the list, last to the last. first指向列表中的第一个节点,last指向最后一个节点。 count is the number of nodes in the linked list. count是链接列表中的节点数。 firstNode is the node I use to hold the sorted list, so anything before it should contain all of the smallest nodes already sorted. firstNode是我用来保存排序列表的节点,因此它之前的所有内容都应包含所有已排序的最小节点。 I keep it on the last node in the sorted portion so I can replace it's link when the next node is found. 我将其保留在已排序部分的最后一个节点上,以便在找到下一个节点时可以替换它的链接。 I have gone over this code several times on paper, and can't seem to find the error. 我已经在纸上多次检查了此代码,似乎找不到错误。

template<class Type>
void unorderedLinkedList<Type>::selectionSort()
{
    nodeType<Type> *tempNode, *traverseNode, *preNode, *firstNode;
    tempNode=first; //initializing Nodes
    traverseNode = first;
    preNode = first;
    for (int i = 0; i < count - 1; i++) //loops 1 time for setting first Node
    {
        if (tempNode->info > traverseNode->link->info) // checks for smallest Node
        {
            preNode = traverseNode;
            tempNode = traverseNode->link;
        }
        traverseNode = traverseNode->link;
    }
    if (tempNode != first) //places smallest Node in position first
    {
        if (tempNode != last) //check if tempNode is last
            preNode->link = tempNode->link;
        else
        {
            preNode->link = NULL;
            last = preNode;
        }
        tempNode->link = first;
        first = tempNode;
    }

    firstNode = first;
    for (int iteration = 2; iteration < count; iteration++) //loop that should place element into the next slot each iteration
    {
        preNode = firstNode;
        tempNode = firstNode->link;
        for (int index = 0; index < count - iteration; index++) //loop that should find the smallest element
        {
            traverseNode = firstNode->link;
            if (traverseNode->link->info < tempNode->info) //checking if node is smaller
            {
                preNode = traverseNode;
                tempNode = traverseNode->link;
            }
            traverseNode = traverseNode->link; //traverse to next node
        }
        if (tempNode != last) //check if node is the last node  
            preNode->link = tempNode->link;
        else
        {
            preNode->link = NULL;
            last = preNode;
        }
        tempNode->link = firstNode->link; //node swap
        firstNode->link = tempNode;
        firstNode = firstNode->link;
    }
} //end selectionSort

This line 这条线

traverseNode = firstNode->link;

needs to be outside of your inner for loop (the one on index ). 需要在内部for循环之外( index上的那个)。 Otherwise you are always just comparing against the same node. 否则,您总是只与同一个节点进行比较。

Edit: You are also missing a test for when you don't have to swap nodes. 编辑:当您不必交换节点时,您也缺少测试。 Your second inner loop and subsequent link manipulation should look like your first loop (in fact, you may be able to simplify and use the same code for both) 您的第二个内部循环和随后的链接操纵应类似于您的第一个循环(实际上,您可能能够简化并为两者使用相同的代码)

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

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