简体   繁体   English

我正在尝试使用插入排序在列表中以排序方式插入节点

[英]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();

Output:输出:

1 : Output of The Code 1代码的输出

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.

相关问题 尝试使用插入排序C#对文件进行排序 - Trying to sort a file using Insertion Sort c# 尝试执行此多维List插入时,我错过了什么? - What am I missing when trying to perform this multidimensional List insert? 尝试根据“订单”值插入/排序列表 - Trying to insert/sort a list based on 'Order' value C#:我是否正在验证文件类型并以正确的方式使用goto? - C#: Am i validating filetype and using goto in a right manner? 如何停止此代码的迭代我正在使用选择排序算法,如果数组已经排序,我希望迭代停止 - How to stop the iterations of this code i am using the selection sort algorithm and i want the iterations to stop if the array is already sorted 我正在尝试使用 .NET 3.1 将 JSON 反序列化为对象列表 - I am trying to deserialize a JSON into a list of objects using .NET 3.1 我正在尝试将记录插入到对象列表中,但在调用该方法时没有插入任何内容。 我究竟做错了什么? - I am trying to insert records into a an object list but nothing gets inserted when I call the method. What am I doing wrong? 我正在尝试插入帐户,但它抛出错误 - I am trying to insert account but it is throwing errors 使用CompareTo以排序的前20种方式显示和未排序的列表 - Displaying and unsorted list in sorted top 20 manner with CompareTo 表中的 IDENTITY_INSERT 错误我什至没有尝试插入? - IDENTITY_INSERT Error in table I am not even trying to Insert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM