This is a comparison of the performance of notable data structures, as measured by the complexity of their logical operations. For a more comprehensive listing of data structures, see List of data structures. The comparisons in this article are organized by abstract data type. As a single concrete data structure may be used to implement many abstract data types, some data structures may appear in multiple comparisons (for example, a hash map can be used to implement an associative array or a set).
A list or sequence is an abstract data type that represents a finite number of ordered values, where the same value may occur more than once. Lists generally support the following operations:
Linked list | Array | Dynamic array | Balanced tree | Random access list | Hashed array tree | |
---|---|---|---|---|---|---|
Indexing | Θ(n) | Θ(1) | Θ(1) | Θ(log n) | Θ(log n)[1] | Θ(1) |
Insert/delete at beginning | Θ(1) | N/A | Θ(n) | Θ(log n) | Θ(1) | Θ(n) |
Insert/delete at end | Θ(1) when last element is known; Θ(n) when last element is unknown |
N/A | Θ(1) amortized | Θ(log n) | Θ(log n) updating | Θ(1) amortized |
Insert/delete in middle | search time + Θ(1)[2][3] | N/A | Θ(n) | Θ(log n) | Θ(log n) updating | Θ(n) |
Wasted space (average) | Θ(n) | 0 | Θ(n)[4] | Θ(n) | Θ(n) | Θ(√n) |
Maps store a collection of (key, value) pairs, such that each possible key appears at most once in the collection. They generally support three operations:[5]
Unless otherwise noted, all data structures in this table require O(n) space.
Data structure | Lookup, removal | Insertion | Ordered | ||
---|---|---|---|---|---|
average | worst case | average | worst case | ||
Association list | O(n) | O(n) | O(1) | O(1) | No |
B-tree[6] | O(log n) | O(log n) | O(log n) | O(log n) | Yes |
Hash table | O(1) | O(n) | O(1) | O(n) | No |
Unbalanced binary search tree | O(log n) | O(n) | O(log n) | O(n) | Yes |
Some map data structures offer superior performance in the case of integer keys. In the following table, let m be the number of bits in the keys.
Data structure | Lookup, removal | Insertion | Space | ||
---|---|---|---|---|---|
average | worst case | average | worst case | ||
Fusion tree | O(log m n) | O(n) | |||
Van Emde Boas tree | O(log log m) | O(log log m) | O(log log m) | O(log log m) | O(m) |
X-fast trie | O(n log m)[lower-alpha 1] | O(log log m) | O(log log m) | O(n log m) | |
Y-fast trie | O(log log m)[lower-alpha 1] | O(log log m)[lower-alpha 1] | O(n) |
A priority queue is an abstract data-type similar to a regular queue or stack. Each element in a priority queue has an associated priority. In a priority queue, elements with high priority are served before elements with low priority. Priority queues support the following operations:
Priority queues are frequently implemented using heaps.
A (max) heap is a tree-based data structure which satisfies the Template:Dfni: for any given node C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C.
In addition to the operations of an abstract priority queue, the following table lists the complexity of two additional logical operations:
Here are time complexities[7] of various heap data structures. Function names assume a min-heap. For the meaning of "O(f)" and "Θ(f)" see Big O notation.
Operation | find-min | delete-min | insert | decrease-key | meld |
---|---|---|---|---|---|
Binary[7] | Θ(1) | Θ(log n) | O(log n) | O(log n) | Θ(n) |
Leftist | Θ(1) | Θ(log n) | Θ(log n) | O(log n) | Θ(log n) |
Binomial[7][8] | Θ(1) | Θ(log n) | Θ(1)[lower-alpha 1] | Θ(log n) | O(log n)[lower-alpha 2] |
Fibonacci[7][9] | Θ(1) | O(log n)[lower-alpha 1] | Θ(1) | Θ(1)[lower-alpha 1] | Θ(1) |
Pairing[10] | Θ(1) | O(log n)[lower-alpha 1] | Θ(1) | o(log n)[lower-alpha 1][lower-alpha 3] | Θ(1) |
Brodal[13][lower-alpha 4] | Θ(1) | O(log n) | Θ(1) | Θ(1) | Θ(1) |
Rank-pairing[15] | Θ(1) | O(log n)[lower-alpha 1] | Θ(1) | Θ(1)[lower-alpha 1] | Θ(1) |
Strict Fibonacci[16] | Θ(1) | O(log n) | Θ(1) | Θ(1) | Θ(1) |
2-3 heap[17] | O(log n) | O(log n)[lower-alpha 1] | O(log n)[lower-alpha 1] | Θ(1) | ? |
Original source: https://en.wikipedia.org/wiki/Comparison of data structures.
Read more |