public void InsertionSort()
{
if (this.Head == null || this.Head.Next == null)
{
return;
}
LinkedListNode curr = this.Head.Next;
LinkedListNode prev = this.Head;
while(curr != null)
{
LinkedListNode next = curr.Next;
this.SortedInsert(curr);
if (curr.Next == next)
{
prev.Next = curr;
prev = curr;
}
else
{
prev.Next = next;
}
curr = next;
}
}
public void SortedInsert(LinkedListNode node)
{
if (this.Head == null || this.Head.Value >= node.Value)
{
node.Next = this.Head;
this.Head = node;
}
else
{
LinkedListNode curr = this.Head;
while(curr.Next != null && curr.Next != node && curr.Next.Value < node.Value)
{
curr = curr.Next;
}
if (curr.Next != node)
{
node.Next = curr.Next;
curr.Next = node;
}
}
}
No comments:
Post a Comment