240 words
1 minutes
Time Complexities of different Data Structures
Strings
- Create: O(n) time to create a string of length n.
- Read: O(1) time to access an individual character.
- Update: O(n) time, as strings are immutable and require creating a new string for updates.
- Delete: O(n) time, requiring creation of a new string without the deleted character.
Arrays
- Create: O(1) for fixed size, O(n) for dynamic size.
- Read: O(1) time to access an element at a specific index.
- Update: O(1) time to update an element at a specific index.
- Delete: O(n) time to delete an element in the middle, O(1) at the end.
Dynamic Arrays
- Create: O(1) to create an empty array, O(n) for n elements.
- Read, Update, Delete: Same as Arrays.
Linked Lists
- Create: O(n) time to create a list with n elements.
- Read/Update/Delete: O(n) to find an element.
Stacks
- Create/Read/Update/Delete: O(1) time for all operations related to the top element.
Queues
- Create/Read/Delete: O(1) time to create, access the front, and dequeue.
- Update: O(n) time for updating an element.
Hash Tables
- Create: O(n) time.
- Read/Update/Delete: O(1) average case, O(n) worst case due to collisions.
Trees
- Create: O(n log n) for balanced trees, O(n^2) for unbalanced.
- Read/Update/Delete: O(log n) in balanced trees, O(n) in unbalanced.
Graphs
- Create: O(n + m) time, where n is nodes and m is edges.
- Read/Update/Delete: O(m) time for edge-related operations.
Heaps
- Create: O(n log n) time.
- Read: O(1) for the min/max element.
- Update/Delete: O(log n) time for these operations.
Tries
- Create: O(n * m) time, where n is number of strings and m is average string length.
- Read/Update/Delete: O(m) time for string operations.
Union Find
- Create: O(n) time.
- Read: O(1) time to find the set representative.
- Update: O(n) time to union two sets.
- Delete: Not applicable.
Time Complexities of different Data Structures
https://rabisiddique.com/posts/time-complexities/Author
Rabi Siddique
Published at
2023-02-13