Skip to content

Commit 958f580

Browse files
committed
Migrate remaining readme files
1 parent f2c9864 commit 958f580

157 files changed

Lines changed: 1631 additions & 5475 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎algorithm_theory/bubble_sort.py‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
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-
"""
1+
def bubble_sort(array: [int]) -> []:
2+
"""Bubble Sort.
83
4+
The simplest sorting algorithm, working by repeatedly swapping the adjacent elements if they are
5+
in wrong order.
96
10-
def bubble_sort(array: [int]) -> []:
11-
"""Repeatedly swap adjacent out-of-order elements, shrinking the unsorted upper bound each pass."""
7+
Example: `[2, 1, 3] -> [1, 2, 3]`
8+
"""
129
length = len(array)
1310

1411
# Return if nothing to sort

‎algorithm_theory/jump_search.py‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
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-
"""
17-
181
import math
192
from typing import Optional
203

214
from algorithm_theory.linear_search import linear_search
225

236

247
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."""
8+
"""Jump Search (https://www.geeksforgeeks.org/jump-search/).
9+
10+
A searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear
11+
search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.
12+
13+
For example, suppose we have an array arr[] of size n and block (to be jumped) size `m`. Then we search
14+
at the indexes `arr[0]`, `arr[m]`, `arr[2m]…..arr[km]`, and so on. Once we find the interval
15+
(`arr[km] < x < arr[(k+1)m]`), we perform a linear search operation from the index `km` to find `x`.
16+
17+
What is the optimal block size to be skipped?
18+
In the worst case, we have to do `n/m` jumps and if the last checked value is greater than the element
19+
to be searched for, we perform `m-1` comparisons more for linear search. Therefore the total number of
20+
comparisons in the worst case will be `((n/m) + m-1)`. The value of the function `((n/m) + m-1)` will be
21+
minimum when `m = √n`. Therefore, the best step size is `m = √n`. (Geeks For Geeks)
22+
"""
2623
length = len(array)
2724
interval = int(math.sqrt(length))
2825
index = interval - 1 # 0 Based

‎algorithm_theory/linear_search.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
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-
"""
6-
71
from typing import Optional
82

93

104
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."""
5+
"""Linear Search.
6+
7+
The most basic search. Start from the leftmost element of the array and one by one compare the target
8+
with each element of the array. If the target matches with an element, return the index.
9+
"""
1210
for index in range(left, right + 1):
1311
if array[index] == target:
1412
return index

0 commit comments

Comments
 (0)