[英]I am trying to Insert a Node in Sorted Manner in a List using Insertion Sort
I am trying to implement Bucket Sort Algorithm in C# on Linked List , but I am getting In correct results.我正在尝试在 C# 中的 Linked List 上实现 Bucket Sort Algorithm,但我得到了正确的结果。 This function should take List and Node in argument and than insert that node at correct ascending order.
此函数应在参数中采用 List 和 Node,然后以正确的升序插入该节点。 I have a list [4, 7, 12, 15] and I want new Node with data 20 to be Inserted in Ascending order such as this list becomes [4,7,12,15,20].But in result I am getting this list[4,7,12,20,15] Please Help me, What's wrong in it.
我有一个列表 [4, 7, 12, 15] 并且我希望以升序插入数据为 20 的新节点,例如此列表变为 [4,7,12,15,20]。但结果我得到这个列表[4,7,12,20,15] 请帮帮我,里面有什么问题。 I guess there is some problem inside while loop.
我猜while循环内部存在一些问题。 All other functions called by this function are totally working perfect
此函数调用的所有其他函数都完美运行
static public void InsertionSort(LinkedList list, Node s)
{
int data = s.getData();
if (list.start.getNext() == null)
list.InsertAtEnd(data);
else
{
int key = 0;
Node temp = list.start;
while (temp.getNext() != null)
{
temp = temp.getNext();
if (data >= temp.getData())
key++;
}
list.InsertAt(key, data);
}
}
Derived Function (Main Function):派生函数(主函数):
LinkedList list = new LinkedList();
int[] array = { 4, 7, 12, 15 };
for (int i = 0; i < array.Length; i++)
list.InsertAtEnd(array[i]);
System.Console.WriteLine("Before Insertion");
list.Display();
Node n = new Node(20);
InsertionSort(list, n);
System.Console.WriteLine("After Insertion");
list.Display();
The setup in the beginning is wrong.一开始的设置是错误的。
int[] array = { 4, 7, 12, 15 };
for (int i = 0; i < array.Length; i++)
list.InsertAtBegin(array[i]);
Because you go over the initial array and add all the items at the beginning, the resulting list is sorted in the wrong order.因为您遍历初始数组并在开头添加所有项目,所以结果列表的排序顺序错误。
To start with the correct sorting, either add at the end:要从正确的排序开始,请在末尾添加:
int[] array = { 4, 7, 12, 15 };
for (int i = 0; i < array.Length; i++)
list.InsertAtEnd(array[i]);
or go over the initial array in the other direction:或在另一个方向遍历初始数组:
int[] array = { 4, 7, 12, 15 };
for (int i = array.length - 1; i >= 0; i--)
list.InsertAtBegin(array[i]);
Also, your insertion algorithm isn't optimal.此外,您的插入算法不是最佳的。
With a linked list, to get to a certain element in the list is expensive because you always have to travel from the start to the correct position.使用链表,到达列表中的某个元素是昂贵的,因为你总是必须从头到尾移动到正确的位置。
Your code first tries to find the correct place to insert the new element and then calls another method InsertAt
which will have to restart traveling the list from the beginning to find the same position again.您的代码首先尝试找到插入新元素的正确位置,然后调用另一个方法
InsertAt
,该方法必须重新从头开始InsertAt
列表以再次找到相同的位置。
Instead, change your code so that you directly insert the new node as soon as you have found an element with a higher value.相反,请更改您的代码,以便您在找到具有更高值的元素后立即直接插入新节点。
Thanks to all of you who tried to help me.感谢所有试图帮助我的人。 I have correct my code.
我已经更正了我的代码。 Now It's producing 100% accurate result.
现在它产生了 100% 准确的结果。
static public void InsertionSort(LinkedList list, int data)
{
Node n = new Node(data);
Node temp = list.start;
Node sNode = null;
while(temp.getNext() !=null)
{
if (data >= temp.getNext().getData())
sNode = temp.getNext();
temp = temp.getNext();
}
if (sNode == null)
list.InsertAtBegin(data);
else
list.InsertAfter(sNode,n);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.