简体   繁体   English

将在LinkedList上进行二进制搜索 <T> 将值插入排序后的值列表中间可以提高性能?

[英]Will a binary search on LinkedList<T> to insert a value into the middle of a sorted value list increase performance?

I need to create a sorted list adding one item at a time. 我需要创建一个排序列表,一次添加一个项目。 So I decided to go with LinkedList<T> , Since it is efficient in insert operations. 所以我决定使用LinkedList<T> ,因为它在插入操作中很有效。 But when finding the proper location, it seems to take much longer time. 但是当找到合适的位置时,它似乎需要更长的时间。 I am using linear search to get the location. 我正在使用线性搜索来获取位置。 If I use binary search to get the proper location using ElementAt method, will it increase the performance? 如果我使用ElementAt方法使用二进制搜索来获取正确的位置,是否会提高性能? According to this , still it is a O(n) operation. 根据这个 ,它仍然是一个O(n)的操作。 What do you think? 你怎么看? If it is so, is there any other better data structure for the work? 如果是这样,还有其他更好的数据结构可用于这项工作吗? Because if I use a different data structure, when inserting a new value to the middle, I will have to shift all the data after that location by one location, which is obviously not desired. 因为如果我使用不同的数据结构,则在向中间插入新值时,将不得不将该位置之后的所有数据移动一个位置,这显然是不希望的。

Your question has a few misconceptions. 您的问题有一些误解。

Will a binary search on LinkedList to insert a value into the middle of a sorted value list increase performance? 在LinkedList上进行二进制搜索以将值插入到已排序的值列表中间是否会提高性能?

Binary search makes sense only on a sorted collection. 二进制搜索仅对排序的集合有意义。 LinkedList<T> isn't one. LinkedList<T>不是一个。 It's an insertion order respecting collection. 这是尊重收藏的插入顺序。 To perform a binary search on a LinkedList<T> : 要对LinkedList<T>执行二进制搜索:

  • you will have to either sort the linked list, which is not only very inefficient (operation on a linked list) but also too much work for mere insertion 您将不得不对链表进行排序,这不仅效率很低(对链表进行操作),而且对于仅插入文件也需要太多工作

  • or you will have to maintain the sort order at the time of insertion itself by inserting in the right position, which is too much work again. 否则在插入时您将不得不通过将其插入正确的位置来维持排序顺序,这又是一件繁重的工作。

From your question what I understand is that you want a sort order respecting collection but insertions has to faster (unlike O(n) characteristic of say, a typical List<T> ). 从您的问题中我了解到,您想要一种尊重集合的排序顺序,但是插入必须更快(这不同于典型的List<T> O(n)特性)。 My suggestion would be to use a sorted collection type in .NET. 我的建议是在.NET中使用排序的集合类型。 There is a SortedSet<T> in system.dll . system.dll没有SortedSet<T> That wouldn't let you have duplicates as it is a set, in which case you can implement a custom SortedBag<T> (or SortedList<T> or whatever name) as shown here . 这不会让您有重复项,因为它是一个集合,在这种情况下,您可以实现自定义SortedBag<T> (或SortedList<T>或任何名称),如下所示

I am using linear search to get the location. 我正在使用线性搜索来获取位置。

My suggestion is that you dont do that. 我的建议是您不要那样做。 Let the collection itself find its location to perform an insert. 让集合本身找到执行插入的位置。 Sorted collection types are best fit here. 排序的收集类型最适合此处。

If I use binary search to get the proper location using ElementAt method, will it increase the performance 如果我使用ElementAt方法使用二进制搜索来获取正确的位置,它将提高性能

ElementAt method doesn't perform binary search. ElementAt方法不执行二进制搜索。 At best it checks if collection type is an IList<T> and uses the indexer accordingly. 充其量它会检查集合类型是否为IList<T>并相应地使用索引器。 No in your case it makes no difference to performance. 在您的情况下,这对性能没有影响。

when inserting a new value to the middle, I will have to shift all the data after that location by one location, which is obviously not desired . 在中间插入一个新值时,我将不得不将该位置之后的所有数据移动一个位置,这显然是不希望的。

You're right, inserting in the middle of array based structures like List<T> is an O(n) operation. 没错,插入基于数组的结构(如List<T> )的中间是O(n)操作。 I'm not sure if you need index operation as well, but that will be costlier. 我不确定您是否也需要索引操作,但这会更昂贵。 You can either have a sorted collection type with efficient inserts which will have no sense of indexes, or you can have an indexed sorted collection type but insertions will be O(n). 您可以具有带有效插入的排序收集类型,而没有索引的意义,或者可以具有带索引的排序收集类型,但是插入将为O(n)。 Its the trade off you have to pay. 这是您必须付出的代价。

For your purpose, you would probably be better off with a SortedDictionary class (or perhaps SortedList) 为了您的目的,您最好使用SortedDictionary类(或也许SortedList)

SortedDictionary gives O(log n) insert and retrieval times. SortedDictionary给出O(log n)的插入和检索时间。

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

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