Skip to content

Commit c7c124d

Browse files
committed
add binary search and some ideas for the guide
1 parent 16be43d commit c7c124d

File tree

5 files changed

+308
-396
lines changed

5 files changed

+308
-396
lines changed

README.md

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ Also consult [Visualgo] for visualizing data structures and algorithms through a
7676

7777
[^2]: 25 problems / year, but it runs from 2015 up to now. The goal is to solve all the problems.
7878

79+
If you're into reading very technical documents —hardcore mode— you can check out the [Python docs], [C C23] Language Standard, and [CUDA] Programming Guide, or [CUDA Best Practices]. These resources are more advanced, but they're excellent for learning about the languages in depth, reimplementing some of the functions, and becoming very familiar with the core concepts from the very source.
80+
81+
[C C23]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf
82+
[CUDA]: https://docs.nvidia.com/cuda/pdf/CUDA_C_Programming_Guide.pdf
83+
[CUDA Best Practices]: https://docs.nvidia.com/cuda/pdf/CUDA_C_Best_Practices_Guide.pdf
84+
[Python docs]: https://docs.python.org/
85+
7986
<!-- LINKS -->
8087

8188
[c]: https://wakatime.com/badge/user/5272a810-7eca-46d6-ae5c-e0a33012c5d9/project/a687b2dd-4067-470b-9c5a-0577a5518880.svg?style=social
@@ -112,57 +119,23 @@ Also consult [Visualgo] for visualizing data structures and algorithms through a
112119

113120
Personally, what I did was to start brushing up on the basics of data structures and algorithms through easy reading [Grokking Algorithms] and [Grokking Data Structures]. Both are pretty updated books from last year, and I just wanted to get a bit more familiar with the ideas.
114121

122+
[Grokking Algorithms]: https://annas-archive.org/md5/6014af49a9c4c513b76f29b74a869e3f
123+
[Grokking Data Structures]: https://annas-archive.org/md5/8116130f95c8cfc0c010a5be29c75cf2
124+
115125
Arrays seems like the most fundamental data structure, so the first thing you should be able to do it's to work with that.
116126

117127
### Arrays
118128

119129
There are two types of arrays: static and dynamic. Static arrays have a fixed size, which means the size must be defined at compile time and cannot be changed during runtime. Dynamic arrays, on the other hand, can grow and shrink in size as needed during program execution. Understanding the difference between these two types is crucial for efficient memory management and performance optimization.
120130

121-
**Static Array in C:**
131+
[DynamicArray class (lines 10-17)](/data_structures/arrays/dynamic.py#L10-L17)
122132

123-
```c
124-
#include <stdio.h>
133+
Use Case: Static arrays are ideal when memory is constrained and the maximum size is known in advance, such as embedded systems or when implementing fixed-size buffers. The size limitation provides safety against buffer overflows.
125134

126-
int main() {
127-
int arr[5] = {1, 2, 3, 4, 5};
128-
int i = 0;
129-
while (i < 5) {
130-
printf("%d ", arr[i]);
131-
i++;
132-
}
133-
printf("\n");
134-
return 0;
135-
}
136-
```
137-
138-
Use Case: Static arrays are suitable when the number of elements is known in advance and does not change, such as storing the days of the week.
139-
140-
**Dynamic Array in Python:**
141-
142-
```python
143-
arr = [1, 2, 3, 4, 5]
144-
print(arr)
145-
arr.append(6)
146-
print(arr)
147-
```
135+
Use Case: Dynamic arrays are ideal when the size of the data is unknown or changes frequently. This implementation shows how Python lists work under the hood, automatically resizing when capacity is reached.
148136

149137
### Binary search
150138

151-
![animation idea](/animations/binary_search.html)
152-
153-
```python
154-
def binary_search(array, target):
155-
low, high = 0, len(array) - 1
156-
while low <= high:
157-
mid = (low + high) // 2 # or low + (high - low) // 2
158-
if array[mid] == target:
159-
return mid
160-
elif array[mid] < target:
161-
low = mid + 1
162-
else:
163-
high = mid - 1
164-
return -1
165-
```
139+
Binary search is a divide-and-conquer algorithm with O(log n) complexity that finds elements in sorted arrays by repeatedly halving the search interval. It's a fast and efficient way to search for elements in large datasets, first or last occurrence, or even the closest element to a target value.
166140

167-
[Grokking Algorithms]: https://annas-archive.org/md5/6014af49a9c4c513b76f29b74a869e3f
168-
[Grokking Data Structures]: https://annas-archive.org/md5/8116130f95c8cfc0c010a5be29c75cf2
141+
[Binary search](/algorithms/binary_search.py#L1-L20)

algorithms/binary_search.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def binary_search(array, target):
2+
"""
3+
Binary search algorithm to find the target element in the array.
4+
Time complexity: O(log n)
5+
Space complexity: O(1)
6+
>>> binary_search([1, 2, 3, 4, 5], 3)
7+
2
8+
>>> binary_search([1, 2, 3, 4, 5], 6)
9+
-1
10+
"""
11+
left, right = 0, len(array) - 1
12+
while left <= right:
13+
mid = (left + right) // 2 # or low + (high - low) // 2
14+
if array[mid] == target:
15+
return mid
16+
elif array[mid] < target:
17+
left = mid + 1
18+
else:
19+
right = mid - 1
20+
return -1

data_structures/array/array.py

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)