Efficient data structure for fast random access, search, insertion and deletion

前端 未结 8 2108
灰色年华
灰色年华 2021-01-31 11:11

I\'m looking for a data structure (or structures) that would allow me keep me an ordered list of integers, no duplicates, with indexes and values in the same range.

I ne

8条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 11:46

    If you're working in .NET, then according to the MS docs http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

    • SortedDictionary and SortedList both have O(log n) for retrieval
    • SortedDictionary has O(log n) for insert and delete operations, whereas SortedList has O(n).

    The two differ by memory usage and speed of insertion/removal. SortedList uses less memory than SortedDictionary. If the SortedList is populated all at once from sorted data, it's faster than SortedDictionary. So it depends on the situation as to which is really the best for you.

    Also, your argument for the Linked List is not really valid as it might be O(1) for the insert, but you have to traverse the list to find the insertion point, so it's really not.

提交回复
热议问题