C# 如何对IntNode列表进行排序
如果我得到的是列表的第一个节点,如何将列表从最小到最高排序 例如: 名单 5,-4,8,12,5,71,13,0 该列表将被返回 -4,0,5,5,8,12,13,71 我已经试过了,但是我想不出来。。。谢谢 我所尝试的:C# 如何对IntNode列表进行排序,c#,C#,如果我得到的是列表的第一个节点,如何将列表从最小到最高排序 例如: 名单 5,-4,8,12,5,71,13,0 该列表将被返回 -4,0,5,5,8,12,13,71 我已经试过了,但是我想不出来。。。谢谢 我所尝试的: class IntNode { public int value { get; set; } public IntNode next { get; set; } public IntNode(int value)
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]";
}
}
这是我的主页,请帮忙。在你的课堂上实现这个界面
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;
}
将所有节点放入列表中,然后使用该方法
首先,您需要清楚地了解排序算法的工作原理。用普通语言写下来会有帮助 从您的代码中,我看到您在列表中漫游,并尝试将当前项与下一项进行比较。这听起来是正确的方法。条件是正确的,但值的移动在您的解决方案中不起作用
var allNodes = new List<IntNode>();
// add nodes to list ...
allNodes.Sort();
排序的基本思想是移动项目。
换言之:
当它尚未排序且下一个元素不为null时,如果当前项大于下一个交换值,则移动到下一个元素并再次执行
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;
其余的是2个while循环和一个用于停止外部循环的布尔变量
编辑:
由于您似乎已经自己解决了这个问题,为了完整起见,这里是我的排序方法版本:
// 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;
}
您能否显示您尝试过的内容,即您尝试进行排序的代码?您是否使用特定的排序算法?你应该用谷歌搜索一下……它们很常见,而且很容易实现。你可能需要实现IComparable来排序it@rory.ap我的尝试离最终产品很远,而且非常复杂和漫长。。。我宁愿在这里得到答案…你的尝试很复杂?让我们看看你试过什么,我们会帮你的。我们不会为您编写代码;我们将帮助您使自己的代码正常工作。一旦你用你需要帮助的代码编辑了这个问题,我将撤销我的否决票。好吧,对数字列表进行排序绝对不是一个新奇的想法……它从远古以来就被彻底覆盖了。它相当简单,就像我说的,实际上只需要5到10行代码。只需谷歌一下。没有必要重新发明轮子。这不是我想要的方式。。。看看我的尝试,我觉得这是一个类,他应该实现自己的排序算法。你可能是对的,艾米。尽管如此,我还是建议实现IComparable,然后在自定义排序算法中使用它。这隐藏了在对象内部进行比较的方式。然后,该算法可以对一个IEnumerable进行操作,这是一个很好的抽象。@OhayonDaniel很高兴听到你找到了它。祝贺你为了完整起见,我发布了我的版本。坚持下去!
// 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;
}
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;
}