Common set C # class that implements the basic operations and complexity

List collection class is a linear sequence table, Add operation is O (1) or O (n), since the dynamic capacity expansion of the List, and not before the expansion, which Add operation is O (1), while the capacity is needed when will copy those elements already exist while adding new elements, this time the Add operation is O (n) a. For Contains method, which is in a linear search, which is a complexity of O (n). BinarySearch and method, which is based on a binary search, whose complexity is O (lg n).

SortedList collection class is an ordered linear table, Add operations is O (n), which Contains method is to find the retrieving elements by half, and therefore the complexity is O (lg n), which Containskey methods also find retrieving elements by half, the complexity of is O (lg n), ContainsValue element is a linear search complexity is O (n).

Dictionary collections are hash tables, Add operation is O (1) or O (n), and for the same reason. Containskey which is O (1), because the hash to search through the elements, rather than through the elements. The method ContainsValue time complexity is O (n), because the internal value by iterating to find key, rather than by a lookup hash. Item [Key] The key to retrieve the property value, the time complexity is O (1).

SortedDictionary collection class is based on the balanced binary tree, which is the Add method is O (lg n), ContainsKey method is O (lg n), and ContainsValue method is O (n).

HashSet collection class comprising not duplicates unordered hash table (Table nonlinear). Add operation is O (1) or O (n), because the same collection class List. Contains method is O (1). Set HashSet is set only implements the ICollection interface, on a separate element access, there are substantial limitations:   

    1, compared with the List, can not use the index to access elements such as: list [1].       

    2, compared with Dictionary, can not access the key element, for example: dic [key], because each data HashSet saves only one, does not use the Key-Value manner, in other words, the Key is HashSet value, if already know Key, also no need to query to acquire value, need to do is check the value already exists.

SortedSet collection class is based on a red-black tree implementation, the Add method is O (lg n), Contains method is O (lg n)

 Currently only relate to Add and so on Contains retrieval method, follow-up may be supplemented on the complexity of the operation and delete.

reference:

Really O (1) do? I think clearly did not?

C # SortedDictionary and SortedList Talking

Do you really understand the dictionary (Dictionary) do?

Guess you like

Origin www.cnblogs.com/ShaYeBlog/p/11525531.html