Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions searches/jump_search.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"""
Pure Python implementation of the jump search algorithm.
This algorithm iterates through a sorted collection with a step of n^(1/2),
until the element compared is bigger than the one searched.
It will then perform a linear search until it matches the wanted number.
If not found, it returns -1.
Jump Search Algorithm

https://en.wikipedia.org/wiki/Jump_search
Jump Search works on sorted arrays by jumping ahead by fixed steps
instead of checking every element sequentially.

Time Complexity:
Best case: O(1)
Average case: O(√n)
Worst case: O(√n)

Space Complexity:
O(1)
"""

import math
Expand All @@ -20,7 +25,7 @@
T = TypeVar("T", bound=Comparable)


def jump_search(arr: Sequence[T], item: T) -> int:

Check failure on line 28 in searches/jump_search.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP047)

searches/jump_search.py:28:5: UP047 Generic function `jump_search` should use type parameters help: Use type parameters
"""
Python implementation of the jump search algorithm.
Return the index if the `item` is found, otherwise return -1.
Expand Down
Loading