Skip to content

Commit

Permalink
Merge pull request #42 from sudipbanfse/patch-1
Browse files Browse the repository at this point in the history
Added more extensive time complexities
  • Loading branch information
theja-m authored Oct 7, 2024
2 parents 84e7021 + f393ef6 commit 8bda9d0
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Data Structures - Arrays/Arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,71 @@

#to implement arrays as a stack
#https://docs.python.org/3/library/collections.html#collections.deque
In Python, lists are implemented as dynamic arrays, and the time complexity for various operations can be described using Big O notation. Here are the common operations and their time complexities:

# <!-- 1. **Indexing (Accessing an element)**
# - **Operation**: `list[i]`
# - **Time Complexity**: O(1)

# 2. **Appending an element**
# - **Operation**: `list.append(x)`
# - **Time Complexity**: Amortized O(1)

# 3. **Inserting an element at the end**
# - **Operation**: `list.insert(len(list), x)`
# - **Time Complexity**: O(1)

# 4. **Inserting an element at the beginning or middle**
# - **Operation**: `list.insert(i, x)`
# - **Time Complexity**: O(n)

# 5. **Deleting an element by index**
# - **Operation**: `del list[i]`
# - **Time Complexity**: O(n)

# 6. **Removing an element by value**
# - **Operation**: `list.remove(x)`
# - **Time Complexity**: O(n)

# 7. **Popping an element from the end**
# - **Operation**: `list.pop()`
# - **Time Complexity**: O(1)

# 8. **Popping an element from the beginning or middle**
# - **Operation**: `list.pop(i)`
# - **Time Complexity**: O(n)

# 9. **Concatenation**
# - **Operation**: `list1 + list2`
# - **Time Complexity**: O(n + m) where n and m are the lengths of the lists

# 10. **Slicing**
# - **Operation**: `list[start:end]`
# - **Time Complexity**: O(k) where k is the length of the slice

# 11. **Checking if an element exists**
# - **Operation**: `x in list`
# - **Time Complexity**: O(n)

# 12. **Length of the list**
# - **Operation**: `len(list)`
# - **Time Complexity**: O(1)

# 13. **Sorting the list**
# - **Operation**: `list.sort()`
# - **Time Complexity**: O(n log n)

# 14. **Reversing the list**
# - **Operation**: `list.reverse()`
# - **Time Complexity**: O(n)

# ### Summary
# - **O(1)**: Indexing, appending, popping from the end, checking length.
# - **O(n)**: Inserting/removing elements (not at the end), deleting by index, removing by value, checking if an element exists, reversing.
# - **O(n log n)**: Sorting.
# - **O(n + m)**: Concatenation.
# - **O(k)**: Slicing. -->




0 comments on commit 8bda9d0

Please sign in to comment.