Skip to content

Commit 508306b

Browse files
docs: add algorithms section and update array examples in README
1 parent 3d6fee1 commit 508306b

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

README.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,17 @@ ms.get_min() # 3
4242
from data_structures.array.static_array import StaticArray
4343
from data_structures.array.dynamic_array import DynamicArray
4444

45-
sa = StaticArray(3)
46-
sa.append(1)
47-
sa.append(2)
45+
sa = StaticArray([1, 2, 3]) # or StaticArray(3) for empty
46+
list(sa) # [1, 2, 3]
47+
sa.pop() # 3
4848
sa.insert(0, 0)
49-
list(sa) # [0, 1, 2]
50-
sa.pop() # 2
51-
sa.remove(0) # 0
49+
sa.remove(1) # 1
5250

53-
da = DynamicArray()
54-
for i in range(10):
51+
da = DynamicArray([1, 2, 3]) # or DynamicArray() for empty
52+
for i in range(4, 11):
5553
da.append(i)
5654
len(da) # 10
57-
da.pop() # 9
55+
da.pop() # 10
5856
da.capacity # 16
5957
```
6058

@@ -84,6 +82,30 @@ dll.pop_back() # 3
8482
list(dll) # [1, 2]
8583
```
8684

85+
## Algorithms
86+
87+
### Searching
88+
89+
| Algorithm | Time | Space | Description |
90+
|---|---|---|---|
91+
| `binary_search` | O(log n) | O(1) | Find element index in sorted sequence, or -1. |
92+
| `lower_bound` | O(log n) | O(1) | Index of first element >= target. |
93+
| `upper_bound` | O(log n) | O(1) | Index of first element > target. |
94+
95+
```python
96+
from algorithms.searching.binary_search import binary_search, lower_bound, upper_bound
97+
98+
binary_search([1, 3, 5, 7, 9], 5) # 2
99+
binary_search([1, 3, 5, 7, 9], 4) # -1
100+
101+
lower_bound([1, 3, 3, 3, 5], 3) # 1 (first >= 3)
102+
upper_bound([1, 3, 3, 3, 5], 3) # 4 (first > 3)
103+
104+
# Works with StaticArray and DynamicArray too
105+
binary_search(StaticArray([1, 2, 3]), 2) # 1
106+
binary_search(DynamicArray([1, 2, 3]), 2) # 1
107+
```
108+
87109
## Requirements
88110

89111
- Python >= 3.13
@@ -129,10 +151,14 @@ data_structures/
129151
base.py # ISinglyLinkedList & IDoublyLinkedList ABCs
130152
singly_linked_list.py # SinglyLinkedList
131153
doubly_linked_list.py # DoublyLinkedList
154+
algorithms/
155+
searching/
156+
binary_search.py # binary_search, lower_bound, upper_bound
132157
tests/
133158
test_arrays.py # pytest suite for array implementations
134159
test_stacks.py # pytest suite for stack implementations
135160
test_linked_lists.py # pytest suite for linked list implementations
161+
test_binary_search.py # pytest suite for binary search
136162
.pre-commit-config.yaml # pre-commit hooks (ruff, ty)
137163
pyproject.toml # project config, tools settings
138164
```

0 commit comments

Comments
 (0)