使用链表的Bubblesort实现仅部分有效

时间:2013-03-25 21:31:39

标签: c# bubble-sort

我正在尝试使用冒泡排序来对项目列表进行排序。 我知道这不是最有效的排序方法;但是,我需要这种方法才能继续使用更好的东西。我当前的代码部分排序列表,但我看不出我做错了什么。

public class ListComponents
{
    public int Priority;
    public string Name;
    public ListComponents Next;
}

public void bubblesort()
{
    ListComponents Current = Head; 
    int temp = 0;
    bool swapped = true;

    while (swapped)
    {
        Print(); // Debug Function to print each swap
        Current = Head.Next; // RESET CURRENT TO HEAD + 1 ON EACH ITERATION?
        swapped = false;
        for (int sort = 0; sort < (size - 1); sort++) //Size defined as # of items in list
        {
            if (Current != null && 
                Current.Next != null && 
                Current.Priority> Current.Next.Priority)
            {
                temp = Current.Priority;
                Current.Priority= Current.Next.Priority;
                Current.Next.Priority= temp;
                Current = Head; // POTENTIAL ISSUE?
                swapped = true;
            }
        }
    }
}

我的调试打印功能显示以下内容,显示值几乎按顺序排列:

List: 4 2 1 3  (Inital List Order)
List: 1 4 2 3  (First Swap)
List: 1 2 4 3  (Second Swap)

问题似乎在于设置“当前”值,尽管我看不出它在哪里工作。

2 个答案:

答案 0 :(得分:11)

我的建议:重新开始。这是一团糟。

这是我在初学者时如何解决这样的问题。首先在pseduo-code中编写代码:

void BubbleSort(ListComponent head)
{
    if the list is of zero length then it is already sorted, so return
    if the list is of length one then it is already sorted, so return
    Otherwise, the list is of length two or more. Therefore we know that
    a first item exists and a second-last item exists.
    Our strategy is: run down the list starting with the first item
    and ending with the second-last item. If at any time the current
    item and the one following it are out of order, swap their elements.
    If we made no swaps then the list is sorted, return. 
    If we made one or more swaps then the list might not be sorted, so 
    run down the list again.
}

好的,现在我们可以开始将其翻译成代码。

void BubbleSort(ListComponent head)
{
    // if the list is of zero length then it is already sorted, so return
    if (head == null) return; 

    // if the list is of length one then it is already sorted, so return
    if (head.Next == null) return; 

    // Otherwise, the list is of length two or more. Therefore we know that
    // a first item exists and a second-last item exists.

    // Our strategy is: run down the list starting with the first item
    // and ending with the second-last item. 

    for (ListComponent current = head; 
        current.Next != null; 
        current = current.Next)
    {

        If at any time the current item and the one following it
        are out of order, swap their elements.
    }

    If we made no swaps then the list is sorted, return. 
    If we made one or more swaps then the list might not be sorted, so 
    run down the list again.
}

好的,我已将部分英语翻译成代码。你可以翻译其余的吗?

答案 1 :(得分:0)

另一个带有2个属性的简单类的例子。这不是数组,而是模拟指针的简单类......只是为了好玩!

class MyLinkedList
{
MyLinkedList nextNode;
int data;

public void OrderListBubbleAlgoritm(ref MyLinkedList head)
{
    bool needRestart = true;
    MyLinkedList actualNode = head; //node Im working with        
    int temp;

    while (needRestart)
    {
        needRestart = false;
        actualNode = head;
        while (!needRestart && actualNode.nextNode != null)
        {
            if (actualNode.nextNode.data >= actualNode.data) //is sorted
            {
                actualNode = actualNode.nextNode;
            }
            else
            {
                //swap the data
                temp = actualNode.data;
                actualNode.data = actualNode.nextNode.data;
                actualNode.nextNode.data = temp;

                needRestart = true;
            }
        }
    }
}
}

请记住仅使用少量数据进行气泡排序。
它的表现是:O(n ^ 2)