Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/angkushsahu/time-complexities
This repository summarizes the best, average, and worst-case time complexities for insertion, deletion, updating, and reading a value in various C++ data structures.
https://github.com/angkushsahu/time-complexities
Last synced: 5 days ago
JSON representation
This repository summarizes the best, average, and worst-case time complexities for insertion, deletion, updating, and reading a value in various C++ data structures.
- Host: GitHub
- URL: https://github.com/angkushsahu/time-complexities
- Owner: angkushsahu
- Created: 2024-08-21T05:10:18.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-08-21T05:14:42.000Z (3 months ago)
- Last Synced: 2024-08-21T06:29:10.102Z (3 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Time Complexities of Operations in Various C++ Data Structures
This table summarizes the best, average, and worst-case time complexities for insertion, deletion, updating, and reading a value in various C++ data structures.
| Data Structure | Operation | Best Case Complexity | Average Case Complexity | Worst Case Complexity |
|------------------|-----------|----------------------|-------------------------|-----------------------|
| **Unordered Map**| Insertion | O(1) | O(1) | O(N) |
| | Deletion | O(1) | O(1) | O(N) |
| | Updating | O(1) | O(1) | O(N) |
| | Reading | O(1) | O(1) | O(N) |
| **Ordered Map** | Insertion | O(log N) | O(log N) | O(log N) |
| | Deletion | O(log N) | O(log N) | O(log N) |
| | Updating | O(log N) | O(log N) | O(log N) |
| | Reading | O(log N) | O(log N) | O(log N) |
| **Unordered Set**| Insertion | O(1) | O(1) | O(N) |
| | Deletion | O(1) | O(1) | O(N) |
| | Updating | O(1) | O(1) | O(N) |
| | Reading | O(1) | O(1) | O(N) |
| **Ordered Set** | Insertion | O(log N) | O(log N) | O(log N) |
| | Deletion | O(log N) | O(log N) | O(log N) |
| | Updating | O(log N) | O(log N) | O(log N) |
| | Reading | O(log N) | O(log N) | O(log N) |
| **Vector** | Insertion | O(1) (amortized) | O(N) (at end) | O(N) (at position) |
| | Deletion | O(1) (at end) | O(N) | O(N) |
| | Updating | O(1) | O(1) | O(1) |
| | Reading | O(1) | O(1) | O(1) |
| **Array** | Insertion | O(N) | O(N) | O(N) |
| | Deletion | O(N) | O(N) | O(N) |
| | Updating | O(1) | O(1) | O(1) |
| | Reading | O(1) | O(1) | O(1) |
| **Stack** | Insertion | O(1) | O(1) | O(1) |
| | Deletion | O(1) | O(1) | O(1) |
| | Updating | O(1) | O(1) | O(1) |
| | Reading | O(1) | O(1) | O(1) |
| **Deque** | Insertion | O(1) | O(1) | O(1) |
| | Deletion | O(1) | O(1) | O(1) |
| | Updating | O(1) | O(1) | O(1) |
| | Reading | O(1) | O(1) | O(1) |
| **Queue** | Insertion | O(1) | O(1) | O(1) |
| | Deletion | O(1) | O(1) | O(1) |
| | Updating | O(1) | O(1) | O(1) |
| | Reading | O(1) | O(1) | O(1) |### Notes:
- **Unordered structures (maps and sets)** rely on hashing, which typically provides O(1) average time but potentially O(N) in the worst case due to collisions.
- **Ordered structures (maps and sets)** are usually implemented as balanced trees (like Red-Black Trees), giving O(log N) for insertions, deletions, updates, and reads.
- **Vectors** have O(1) for amortized insertion at the end but O(N) when resizing or inserting at arbitrary positions.
- **Arrays** have O(1) access time for reading and updating, but O(N) for insertion and deletion due to the need to shift elements.
- **Stacks, queues, and deques** typically allow O(1) operations for insertion, deletion, and access due to their specific use cases.