如何对IntNode列表进行排序

时间:2016-11-04 13:26:23

标签: c#

class IntNode
    {
        public int value { get; set; }
        public IntNode next { get; set; }

        public IntNode(int value)
        {
            this.value = value;
            this.next = null;
        }

        public IntNode(int value, IntNode next)
        {
            this.value = value;
            this.next = next;
        }

        public bool HasNext ()
        {
            return (this.next != null);
        }

        public override string ToString()
        {
            if (this.HasNext())
                return this.value + " --> " + this.next;
            else
                return this.value + ". [end]";
        }
    }

如果我获取列表的第一个节点,如何从最小到最高排序列表?

示例:

列表

  

5,-4,8,12,5,71,13,0

将返回该列表

  

-4,0,5,5,8,12,13,71

我试过这样做,但我无法弄明白......谢谢!

我尝试过:

static IntNode Sort (IntNode head)
        {
            IntNode pos = head.next;
            IntNode max = head;
            IntNode current = head;

            while (current != null)
            {
                while (pos != null)
                {
                    if (pos.value > max.value) max = pos;
                    pos = pos.next;
                }
                head.next = max.next;
                max.next = head;
                head = max;
                pos = current;
                max = current;
                current = current.next;
            }
            return head;
        }

这是我的归属词,所以请帮忙。

2 个答案:

答案 0 :(得分:1)

首先,您需要明确您对排序算法应如何工作的想法。它有助于用普通语言写下来。

从您的代码中我看到您漫无目的地浏览列表并尝试将当前项目与下一项目进行比较。这听起来像是正确的方法。条件是正确的,但值的移动在你的解决方案中不起作用。

if (pos.value > max.value)

排序的基本思路是移动物品。 换句话说:

未排序 下一个元素非空如果 当前项目大于下一步交换,请转到下一个元素并再次执行。

// in the beginning you only need one helping IntNode variable
IntNode current = head;
// and a flag to stop the sorting
bool sorted = false;

我建议切换值

//    the condition for the comparison
if (current.value > current.next.value)
{
    int temp = current.value;
    current.value = current.next.value;
    current.next.value = temp;
    // set the flag the a sort procedure has been made as indicator that it might still not be sorted
    sorted = false;
}

其余的是2个while循环和一个布尔变量来停止外循环。

修改

由于您似乎已经自己想出来了,为了完整起见,这是我的排序方法版本:

public static IntNode Sort(IntNode head)
{
    IntNode current = head;
    bool sorted = false;

    while (!sorted)
    {
        // assume that at this run you actually finished your sorting job
        sorted = true;
        // set the current element to the first position
        current = head;

        while (current.next != null)
        {
            if (current.value > current.next.value)
            {
                int temp = current.value;
                current.value = current.next.value;
                current.next.value = temp;
                // apparently you found an element that was not sorted, so you might not be done yet
                sorted = false;
            }
            // move one element forward
            current = current.next;
        }
    }
    return head;
}

答案 1 :(得分:0)

在班级上实施IComparable界面。

class IntNode : IComparable {
    // ...

    public int CompareTo(object obj) {
        if (obj == null) return 1;

        IntNode otherNode = obj as IntNode ;
        if (otherNode != null) 
            return this.value.CompareTo(otherNode.value);
        else
           throw new ArgumentException("Object is not an IntNode");
    }
}

将所有节点放入列表中,然后使用Sort方法。

var allNodes = new List<IntNode>();
// add nodes to list ...
allNodes.Sort();