algorithms package
Submodules
Module contents
Algorithms
Doc on algo.
- algorithms.binary_search(target: Any, items: List[Any]) int | None[source]
Binary search quickly finds a target value within a sorted collection by repeatedly dividing the search space in half, resulting in logarithmic time complexity.
- Parameters:
target (Any) – The value to search for.
items (List[Any]) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.bubble_sort(arr: List[Any]) List[Any][source]
Sorts a list of integers using the Bubble Sort algorithm.
- Parameters:
arr – The list of items to be sorted.
- Returns:
The sorted list of items.
- Return type:
List[Any]
- algorithms.exponential_search(target: Any, items: List[Any]) int | None[source]
Perform exponential search to find the index of the target element in the given sorted array.
- Parameters:
target (Any) – The value to search for.
items (List[Any]) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.fibonacci_search(target: int, items: List[int]) int | None[source]
Perform fibonacci search to find the index of the target element in the given sorted array.
- Parameters:
target (int) – The value to search for.
items (List[int]) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.interpolation_search(target: int | float | complex, items: List[int | float | complex]) int | None[source]
Perform Interpolation Search to find the index of the target element in the given sorted array.
Interpolation search is an algorithm for finding a specific value within an array that is sorted in ascending order. It works by making a linear interpolation to estimate the position of the target value within the array, then performs binary search in the estimated range. This algorithm is most effective when the elements in the array are uniformly distributed.
- Parameters:
target (int | float | complex) – The value to search for.
items (List[int | float | complex]) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.jump_search(target: Any, items: List[Any]) int | None[source]
Perform Jump Search to find the index of the target element in the given sorted array.
It works on the principle of linear search but improves the efficiency by jumping a fixed number of steps ahead in each iteration, thus reducing the number of comparisons. This algorithm requires the array to be sorted beforehand.
- Parameters:
target (Any) – The target element to search for.
items (List[Any]) – The sorted list or array to search in.
- Returns:
The index of the target element if found, else None.
- Return type:
Optional[int]
- algorithms.linear_search(target: Any, items: List[Any]) int | None[source]
Linear search sequentially scans through a collection, comparing each element with the target value until a match is found or the end of the collection is reached, making it suitable for small or unsorted datasets.
- Parameters:
target (Any) – The value to search for.
items (Any) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.sentinel_linear_search(target: Any, items: List[Any]) int | None[source]
Sentinel linear search optimizes the standard linear search algorithm by reducing comparisons within the loop, enhancing its efficiency for searching unsorted collections.
- Parameters:
target (Any) – The value to search for.
items (Any) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]
- algorithms.ternary_search(target: Any, items: List[Any]) int | None[source]
Perform ternary search to find the index of the target element in the given sorted array.
- Parameters:
target (Any) – The value to search for.
items (List[Any]) – The list of items to search.
- Returns:
The index of the target if found, or None if not present.
- Return type:
Optional[int]