【问题标题】:I am trying to Insert a Node in Sorted Manner in a List using Insertion Sort我正在尝试使用插入排序在列表中以排序方式插入节点
【发布时间】:2019-12-03 16:49:15
【问题描述】:

我正在尝试在 Linked List 上的 C# 中实现桶排序算法,但我得到了正确的结果。此函数应将 List 和 Node 作为参数,然后以正确的升序插入该节点。 我有一个列表 [4, 7, 12, 15],我希望以升序插入数据为 20 的新节点,例如此列表变为 [4,7,12,15,20]。但结果我得到此列表[4,7,12,20,15] 请帮助我,其中有什么问题。我猜while循环里面有一些问题。此函数调用的所有其他函数都完美无缺


    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);
                }
            }

派生函数(主函数):

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();

输出:

1:Output of The Code

【问题讨论】:

  • 请...而不是链接到您的代码图像(这确实不能帮助我们帮助您),只需将您的代码复制并粘贴到您的问题中即可。
  • 好多了!谢谢
  • 谢谢,我已添加代码,请查看
  • 您是否遇到错误或异常?你能在这里添加一些日志记录吗?
  • 下一步:当你说:there is some problem In Insertion Sort Function时,请告诉我们问题出在哪里。 some problem 对我们没有帮助。如果有错误消息,请向我们显示错误。或者,如果代码运行没有错误,则向我们展示结果,以及您想要的结果。

标签: c# linked-list insertion-sort bucket-sort


【解决方案1】:

一开始的设置是错误的。

int[] array = { 4, 7, 12, 15 };
for (int i = 0; i < array.Length; i++)
    list.InsertAtBegin(array[i]);

因为您遍历了初始数组并在开头添加了所有项目,所以结果列表的排序顺序错误。

要从正确的排序开始,请在末尾添加:

int[] array = { 4, 7, 12, 15 };
for (int i = 0; i < array.Length; i++)
    list.InsertAtEnd(array[i]);

或者从另一个方向遍历初始数组:

int[] array = { 4, 7, 12, 15 };
for (int i = array.length - 1; i >= 0; i--)
    list.InsertAtBegin(array[i]);

另外,您的插入算法不是最优的。

对于链表,要到达列表中的某个元素是昂贵的,因为您总是必须从开始到正确的位置。

您的代码首先尝试找到插入新元素的正确位置,然后调用另一个方法InsertAt,这将不得不从头开始遍历列表以再次找到相同的位置。

改为更改代码,以便在找到具有更高值的元素后立即插入新节点。

【讨论】:

  • InsertAtEnd 和 InsertATBegining 都试过了,但还是报错
【解决方案2】:

感谢所有试图帮助我的人。我有正确的代码。现在它正在产生 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);
        }

【讨论】:

  • 您总是遍历整个列表并将要插入的值与列表中的每个值进行比较。一旦找到一个小于您要插入的值的值,您就可以停止 while 循环。另外,我怀疑 list.start 对于空列表可能为空?然后你可以得到 NullReferenceExceptions 并且你会在搜索正确的插入位置时跳过第一项。
  • 乍一看我怀疑如果你有一个只包含一个数字的 LinkedList 并且你尝试插入一个更大的数字会出错。
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多