Skip to content

Commit f2c9864

Browse files
committed
Migrate algorithm_theory folder
1 parent 7381a45 commit f2c9864

15 files changed

Lines changed: 154 additions & 262 deletions

File tree

‎.claude/settings.local.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"permissions": {
33
"allow": [
44
"Bash(grep *)",
5-
"Bash(ls -1 *.py)"
5+
"Bash(ls *)",
6+
"Bash(just *)"
67
]
78
}
89
}

‎algorithm_theory/README.md‎

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

‎algorithm_theory/binary_search.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
# Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
2-
# covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
3-
# the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
4-
# the interval is empty.
1+
"""Binary Search
2+
3+
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
4+
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
5+
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
6+
the interval is empty.
7+
"""
8+
59
from typing import Optional
610

711

812
def binary_search_iterative(array: [int], target: int) -> Optional[int]:
13+
"""Narrow the [left, right] bounds by half each iteration until target is found or the bounds cross."""
914
left = 0
1015
right = len(array) - 1
1116

@@ -22,6 +27,7 @@ def binary_search_iterative(array: [int], target: int) -> Optional[int]:
2227

2328

2429
def binary_search_recursive(array: [int], left: int, right: int, target: int) -> Optional[int]:
30+
"""Recurse into the left or right sub-array each call until target is found or the bounds cross."""
2531
if right >= left:
2632
mid = int((left + right) / 2)
2733

‎algorithm_theory/bubble_sort.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
# Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in
2-
# wrong order.
1+
"""Bubble Sort
2+
3+
The simplest sorting algorithm, working by repeatedly swapping the adjacent elements if they are
4+
in wrong order.
5+
6+
Example: `[2, 1, 3] -> [1, 2, 3]`
7+
"""
38

49

510
def bubble_sort(array: [int]) -> []:
11+
"""Repeatedly swap adjacent out-of-order elements, shrinking the unsorted upper bound each pass."""
612
length = len(array)
713

814
# Return if nothing to sort

‎algorithm_theory/jump_search.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
# Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search)
2-
# by jumping ahead by fixed steps or skipping some elements in place of searching all elements.
3-
#
4-
# step size is m = √n.
1+
"""Jump Search (https://www.geeksforgeeks.org/jump-search/).
2+
3+
A searching algorithm for sorted arrays. The basic idea
4+
is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of
5+
searching all elements.
6+
7+
For example, suppose we have an array arr[] of size n and block (to be jumped) size `m`. Then we search at the
8+
indexes `arr[0]`, `arr[m]`, `arr[2m]…..arr[km]`, and so on. Once we find the interval (`arr[km] < x < arr[(k+1)m]`),
9+
we perform a linear search operation from the index `km` to find the element `x`.
10+
11+
What is the optimal block size to be skipped?
12+
In the worst case, we have to do `n/m` jumps and if the last checked value is greater than the element to be searched
13+
for, we perform `m-1` comparisons more for linear search. Therefore the total number of comparisons in the worst case
14+
will be `((n/m) + m-1)`. The value of the function `((n/m) + m-1)` will be minimum when `m = √n`. Therefore, the best
15+
step size is `m = √n`. (Geeks For Geeks)
16+
"""
517

618
import math
719
from typing import Optional
@@ -10,6 +22,7 @@
1022

1123

1224
def jump_search(array: [int], target: int) -> Optional[int]:
25+
"""Jump ahead by blocks of size √n, then linear search within the block containing target."""
1326
length = len(array)
1427
interval = int(math.sqrt(length))
1528
index = interval - 1 # 0 Based

‎algorithm_theory/linear_search.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from typing import Optional
1+
"""Linear Search.
2+
3+
The most basic search. Start from the leftmost element of the array and one by one compare the target with each
4+
element of the array. If the target matches with an element, return the index.
5+
"""
26

3-
# The most basic search. Start from the leftmost element of the array and one by one compare the target with each
4-
# element of the array. If the target matches with an element, return the index.
7+
from typing import Optional
58

69

710
def linear_search(array: [int], left: int, right: int, target: int) -> Optional[int]:
11+
"""Scan array[left:right] one element at a time for target."""
812
for index in range(left, right + 1):
913
if array[index] == target:
1014
return index

‎arrays/README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def first_not_repeating_character(s: str) -> str:
352352
if char not in checked_chars and s.index(char) == s.rindex(char):
353353
return char
354354
checked_chars.add(char)
355-
return '_'
355+
return "_"
356356
```
357357

358358
With Array Slices (less optimal):
@@ -362,10 +362,10 @@ def first_not_repeating_character(s: str) -> str:
362362
length = len(s)
363363
index = 0
364364
while index < length:
365-
if s[index] not in s[:index] and s[index] not in s[index + 1:]:
365+
if s[index] not in s[:index] and s[index] not in s[index + 1 :]:
366366
return s[index]
367367
index += 1
368-
return '_'
368+
return "_"
369369
```
370370

371371
## First Pivot Index
@@ -500,7 +500,6 @@ def largest_number_at_least_twice_of_others(nums: [int]) -> int:
500500
if next_largest is None or (nums[next_largest] * 2) <= nums[largest]:
501501
return largest
502502
return -1
503-
504503
```
505504

506505
With array manipulation and built-in functions. Slightly slower but easier to read
@@ -596,7 +595,9 @@ Input:
596595
Output: `[1,2,2,3,5,6]`
597596

598597
```python
599-
def merge_sorted_array(nums1: List[int], nums1_length: int, nums2: List[int], nums2_length: int) -> List[int]:
598+
def merge_sorted_array(
599+
nums1: List[int], nums1_length: int, nums2: List[int], nums2_length: int
600+
) -> List[int]:
600601
index_nums1 = nums1_length - 1
601602
index_nums2 = nums2_length - 1
602603
current_index = nums1_length + nums2_length - 1
@@ -905,6 +906,7 @@ def rotate_array_in_place(nums: List[int], k: int) -> List[int]:
905906
reverse(nums, k, length - 1)
906907
return nums
907908

909+
908910
def reverse(arr: List, start, end):
909911
while start < end:
910912
arr[start], arr[end] = arr[end], arr[start]
@@ -1095,9 +1097,9 @@ def third_max(nums: List[int]) -> int:
10951097
if len(unique_nums) < 3:
10961098
return max(unique_nums)
10971099

1098-
unique_nums.remove(max(unique_nums)) # 1st
1099-
unique_nums.remove(max(unique_nums)) # 2nd
1100-
return max(unique_nums) # 3rd
1100+
unique_nums.remove(max(unique_nums)) # 1st
1101+
unique_nums.remove(max(unique_nums)) # 2nd
1102+
return max(unique_nums) # 3rd
11011103
```
11021104

11031105
## Two Sum

‎binary_tree/README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ def binary_tree_paths(root_node: TreeNode) -> str:
4141
paths = []
4242
if not root_node:
4343
return paths
44-
get_path(root_node, '', paths)
44+
get_path(root_node, "", paths)
4545
return paths
4646

47+
4748
def get_path(node: TreeNode, path: str, paths: []) -> str:
4849
if not node.left and not node.right:
4950
paths.append(path + str(node.val))
5051
return
5152
if node.left:
52-
get_path(node.left, path + str(node.val) + '->', paths)
53+
get_path(node.left, path + str(node.val) + "->", paths)
5354
if node.right:
54-
get_path(node.right, path + str(node.val) + '->', paths)
55+
get_path(node.right, path + str(node.val) + "->", paths)
5556
```
5657

5758
## Find Mode
@@ -701,7 +702,7 @@ Assume a BST is defined as follows:
701702
- Both the left and right subtrees must also be binary search trees.
702703

703704
```python
704-
def valid_bst(root: TreeNode, min_value=float('-inf'), max_value=float('inf')) -> bool:
705+
def valid_bst(root: TreeNode, min_value=float("-inf"), max_value=float("inf")) -> bool:
705706
if not root:
706707
return True
707708

0 commit comments

Comments
 (0)