Time complexity of common data structures
The complexity of programs can be divided into time complexity and space complexity. We can literally see their meanings. Next we mainly look at the time complexity of a set, which basically includes. net, huh, huh!
Data Structure |
Add |
Find |
Delete |
Getbyindex |
Array (T []) |
O (N) |
O (N) |
O (N) |
O (1) |
Linked List (Revoke list <t>) |
O (1) |
O (N) |
O (N) |
O (N) |
Resizable array list (List <t>) |
O (1) |
O (N) |
O (N) |
O (1) |
Stack (Stack <t>) |
O (1) |
- |
O (1) |
- |
Queue (Queue <t>) |
O (1) |
- |
O (1) |
- |
Hash table (Dictionary <K, T>) |
O (1) |
O (1) |
O (1) |
- |
Tree-based dictionary (Sorteddictionary <K, T>) |
O (log n) |
O (log n) |
O (log n) |
- |
Hash Table based set (Hashset <t>) |
O (1) |
O (1) |
O (1) |
- |
Tree based set (Sortedset <t>) |
O (log n) |
O (log n) |
O (log n) |
- |
How to select a Data Structure
Array (T [])
- When the number of elements is fixed and the submark is required.
Linked List (Revoke list <t>)
- When an element needs to be added at both ends of the list. Otherwise, use list <t>.
Resizable array list (List <t>)
- When the number of elements is not fixed and you need to use the lower mark.
Stack (Stack <t>)
- When you need to implement LIFO (last in first out.
Queue (Queue <t>)
- When first in first out is required.
Hash table (Dictionary <K, T>)
- When you need to use key-value pairs to quickly add and search, and the elements do not have a specific order.
Tree-based dictionary (Sorteddictionary <K, T>)
- When you need to use key-value pairs to quickly add and search, and the elements are sorted by key.
Hash Table based set (Hashset <t>)
- When you need to save a unique set of values, and the elements do not have a specific order.
Tree based set (Sortedset <t>)
- When you need to save a set of unique values and the elements need to be sorted.
Thanks
Original articleDennis GaoI posted my blog on the blog site. I just added it to my favorites.
This article from: http://www.cnblogs.com/gaochundong/p/3813252.html
References:
- Test the data structure-Part 1: Data Structure Overview
- Test the data structure-Part 2: queue, stack, and hash table
Time complexity of common data structures