# Comparison of data structures

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).

## Lists

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:
* peek: access the element at a given index.
* insert: insert a new element at a given index. When the index is zero, this is called prepending; when the index is the last index in the list it is called appending.
* delete: remove the element at a given index.
Comparison of list data structures :
| | Peek (index) | Mutate (insert or delete) at … | Mutate (insert or delete) at … | Mutate (insert or delete) at … | Excess space, average |
| | Peek (index) | Beginning | End | Middle | Excess space, average |
| Linked list | Θ(n) | Θ(1) | Θ(1), known end element; Θ(n), unknown end element | Θ(n) | Θ(n) |
| Array | Θ(1) | — | — | — | 0 |
| Dynamic array | Θ(1) | Θ(n) | Θ(1) amortized | Θ(n) | Θ(n) |
| Balanced tree | Θ(log n) | Θ(log n) | Θ(log n) | Θ(log n) | Θ(n) |
| Random-access list | Θ(log n) | Θ(1) | — | — | Θ(n) |
| Hashed array tree | Θ(1) | Θ(n) | Θ(1) amortized | Θ(n) | Θ(√n) |

## Maps

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:
* Insert: add a new (key, value) pair to the collection, mapping the key to its new value. Any existing mapping is overwritten. The arguments to this operation are the key and the value.
* Remove: remove a (key, value) pair from the collection, unmapping a given key from its value. The argument to this operation is the key.
* Lookup: find the value (if any) that is bound to a given key. The argument to this operation is the key, and the value is returned from the operation.
Unless otherwise noted, all data structures in this table require O(n) space.
| Data structure | Lookup, removal | Lookup, removal | Insertion | Insertion | Ordered |
| Data structure | average | worst case | average | worst case | Ordered |
| Association list | O(n) | O(n) | O(1) | O(1) | No |
| B-tree | 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 |

### Integer keys

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 | Lookup, removal | Insertion | Insertion | Space |
| Data structure | average | worst case | average | worst case | Space |
| 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) | [?] | O(log log m) | O(log log m) | O(n log m) |
| Y-fast trie | O(log log m) | [?] | O(log log m) | [?] | O(n) |

## Priority queues

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:
* insert: add an element to the queue with an associated priority.
* find-max: return the element from the queue that has the highest priority.
* delete-max: remove the element from the queue that has the highest priority.
Priority queues are frequently implemented using heaps.

### Heaps

A (max) heap is a tree-based data structure which satisfies the heap property: 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:
* increase-key: updating a key.
* meld: joining two heaps to form a valid new heap containing all the elements of both, destroying the original heaps.
Here are time complexities of various heap data structures. The abbreviation am. indicates that the given complexity is amortized, otherwise it is a worst-case complexity. For the meaning of "O(f)" and "Θ(f)" see Big O notation. Names of operations assume a max-heap.
| Operation | find-max | delete-max | increase-key | insert | meld | make-heap |
| Binary | Θ(1) | Θ(log n) | Θ(log n) | Θ(log n) | Θ(n) | Θ(n) |
| Skew | Θ(1) | O(log n) am. | O(log n) am. | O(log n) am. | O(log n) am. | Θ(n) am. |
| Leftist | Θ(1) | Θ(log n) | Θ(log n) | Θ(log n) | Θ(log n) | Θ(n) |
| Binomial | Θ(1) | Θ(log n) | Θ(log n) | Θ(1) am. | Θ(log n) | Θ(n) |
| Skew binomial | Θ(1) | Θ(log n) | Θ(log n) | Θ(1) | Θ(log n) | Θ(n) |
| 2–3 heap | Θ(1) | O(log n) am. | Θ(1) | Θ(1) am. | O(log n) | Θ(n) |
| Bottom-up skew | Θ(1) | O(log n) am. | O(log n) am. | Θ(1) am. | Θ(1) am. | Θ(n) am. |
| Pairing | Θ(1) | O(log n) am. | o(log n) am. | Θ(1) | Θ(1) | Θ(n) |
| Rank-pairing | Θ(1) | O(log n) am. | Θ(1) am. | Θ(1) | Θ(1) | Θ(n) |
| Fibonacci | Θ(1) | O(log n) am. | Θ(1) am. | Θ(1) | Θ(1) | Θ(n) |
| Strict Fibonacci | Θ(1) | Θ(log n) | Θ(1) | Θ(1) | Θ(1) | Θ(n) |
| Brodal | Θ(1) | Θ(log n) | Θ(1) | Θ(1) | Θ(1) | Θ(n) |
* 1 2 3 Amortized time.
* ↑ make-heap is the operation of building a heap from a sequence of n unsorted elements. It can be done in Θ(n) time whenever meld runs in O(log n) time (where both complexities can be amortized). Another algorithm achieves Θ(n) for binary heaps.
* 1 2 3 For persistent heaps (not supporting increase-key), a generic transformation reduces the cost of meld to that of insert, while the new cost of delete-max is the sum of the old costs of delete-max and meld. Here, it makes meld run in Θ(1) time (amortized, if the cost of insert is) while delete-max still runs in O(log n). Applied to skew binomial heaps, it yields Brodal-Okasaki queues, persistent heaps with optimal worst-case complexities.
* ↑ Lower bound of $\Omega (\log \log n),$ upper bound of $O(2^{2{\sqrt {\log \log n}}}).$
* 1 2 Brodal queues and strict Fibonacci heaps achieve optimal worst-case complexities for heaps. They were first described as imperative data structures. The Brodal-Okasaki queue is a persistent data structure achieving the same optimum, except that increase-key is not supported.