简体   繁体   English

给定一个LinkedList,到底是先添加元素再排序,还是直接排序好?

[英]Given a LinkedList, is it better to add elements to the end and then sort, or add the elements in a sorted manner directly?

Which would be faster when using a LinkedList?使用 LinkedList 哪个会更快? I haven't studied sorting and searching yet.我还没有研究排序和搜索。 I was thinking adding them directly in a sorted manner would be faster, as manipulating the nodes and pointers afterward intuitively seems to be very expensive.我在想以排序方式直接添加它们会更快,因为之后直观地操作节点和指针似乎非常昂贵。 Thanks.谢谢。

In general, using a linked list has many difficulties and is very expensive.一般来说,使用链表有很多困难并且非常昂贵。

i)In the usual case, if you want to add values to the sorted linked list, you must go through the following steps: i)在通常情况下,如果要向排序链表添加值,则必须通过以下步骤 go:

  1. If Linked list is empty then make the node as head and return it.如果链表为空,则将该节点设为头并返回。
  2. If the value of the node to be inserted is smaller than the value of the head node, then insert the node at the start and make it head.如果要插入的节点的值小于头节点的值,则在开始处插入该节点并使其成为头节点。
  3. In a loop, find the appropriate node after which the input node is to be inserted.在循环中,找到要插入输入节点的适当节点。 To find the appropriate node start from the head, keep moving until you reach a node GN who's value is greater than the input node.要从头部开始找到合适的节点,请继续移动,直到到达值大于输入节点的节点 GN。 The node just before GN is the appropriate node. GN 之前的节点是适当的节点。
  4. Insert the node after the appropriate node found in step 3. the Time Complexity in this case is O(n).在步骤 3 中找到的适当节点之后插入节点。在这种情况下,时间复杂度为 O(n)。 ( for each element ) but don't forget to sort the linked list before adding other elements in a sorted manner; (对于每个元素)但不要忘记在以排序方式添加其他元素之前对链表进行排序; This means you have to pay extra cost to sort the linked list.这意味着您必须支付额外的费用来对链表进行排序。

ii ) but if you want to add elements to the end of the linked list and then sort them the situation depends on the way of sorting. ii ) 但是如果你想将元素添加到链表的末尾然后对它们进行排序,情况取决于排序方式。 for example you can use merge sort that has O(n*log n) time complexity: and even you can use insertion sort with O(n^2) time complexity.例如,您可以使用具有 O(n*log n) 时间复杂度的合并排序:甚至可以使用具有 O(n^2) 时间复杂度的插入排序。 note : How to sort is a separate issue .注意:如何排序是一个单独的问题。

As you have already noticed, It is not easy to talk about which method is faster and it depends on the conditions of the problem, such as the number of elements of the link list and the elements needed to be added, or whether the cost of initial sorting is considered or not!正如您已经注意到的那样,要谈论哪种方法更快并不容易,这取决于问题的条件,例如链接列表的元素数量和需要添加的元素,或者是否成本是否考虑初始排序!

You have presented two options:您提出了两个选项:

  1. Keep the linked list sorted by inserting each new node at its sorted location.通过在其排序位置插入每个新节点来保持链表排序。

  2. Insert each new node at the end (or start) of the linked list and when all nodes have been added, sort the linked list在链表的末尾(或开头)插入每个新节点,当所有节点都添加完毕后,对链表进行排序

The worst case time complexity for the first option occurs when the list has each time to be traversed completely to find the position where a new node is to be inserted.第一个选项的最坏情况时间复杂度发生在每次必须完全遍历列表以找到要插入新节点的 position 时。 In that case the time complexity is O(1+2+3+...+n) = O(n²)在这种情况下,时间复杂度为 O(1+2+3+...+n) = O(n²)

The worst time complexity for the second option is O(n) for inserting n elements and then O(nlogn) for sorting the linked list with a good sorting algorithm.第二个选项最差的时间复杂度是 O(n) 用于插入n 个元素,然后 O(nlogn) 用于使用良好的排序算法对链表进行排序。 So in total the sorting algorithm determines the overall worst time complexity, ie O(nlogn).所以总的来说,排序算法决定了整体最差的时间复杂度,即 O(nlogn)。

So the second option has the better time complexity.所以第二个选项具有更好的时间复杂度。

Merge sort has a complexity of O(nlogn) and is suitable for linked list.归并排序的复杂度为 O(nlogn),适用于链表。

If your data has a limited range, you can use radix sort and achieve O(kn) complexity, where k is log(range size).如果您的数据范围有限,您可以使用基数排序并实现 O(kn) 复杂度,其中k是 log(范围大小)。

In practice it is better to insert elements in a vector (dynamic array), then sort the array, and finally turn the array into a list.在实践中,最好在向量(动态数组)中插入元素,然后对数组进行排序,最后将数组变成列表。 This will almost certainly give better running times.这几乎肯定会提供更好的运行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM