New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jump search #2415
Merged
+30
−4
Merged
Jump search #2415
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f99120
Merge remote-tracking branch 'upstream/master'
BriseBalloches b5769f7
Merge remote-tracking branch 'upstream/master'
BriseBalloches 3708397
Merge remote-tracking branch 'upstream/master'
BriseBalloches 4cfb62f
Merge branch 'master' of https://github.com/TheAlgorithms/Python
BriseBalloches 86b62a9
Merge remote-tracking branch 'upstream/master'
BriseBalloches 28a56c8
Merge remote-tracking branch 'upstream/master'
BriseBalloches fb2b5ba
Merge remote-tracking branch 'upstream/master'
BriseBalloches 2750f60
jump_search: doctest, docstring, type hint, inputs
BriseBalloches 0ab3772
jumpsearch.py: case number not found
BriseBalloches 25259cc
trailing whitespace jump search
BriseBalloches File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -1,7 +1,28 @@ | ||
| """ | ||
| 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. | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
|
|
||
| def jump_search(arr, x): | ||
| def jump_search(arr: list, x: int) -> int: | ||
cclauss
Member
|
||
| """ | ||
| Pure Python implementation of the jump search algorithm. | ||
| Examples: | ||
| >>> jump_search([0, 1, 2, 3, 4, 5], 3) | ||
| 3 | ||
| >>> jump_search([-5, -2, -1], -1) | ||
| 2 | ||
| >>> jump_search([0, 5, 10, 20], 8) | ||
| -1 | ||
cclauss
Member
|
||
| >>> jump_search([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610], 55) | ||
| 10 | ||
| """ | ||
|
|
||
| n = len(arr) | ||
| step = int(math.floor(math.sqrt(n))) | ||
| prev = 0 | ||
| @@ -21,6 +42,11 @@ def jump_search(arr, x): | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] | ||
| x = 55 | ||
| print(f"Number {x} is at index {jump_search(arr, x)}") | ||
| user_input = input("Enter numbers separated by a comma:\n").strip() | ||
| arr = [int(item) for item in user_input.split(",")] | ||
| x = int(input("Enter the number to be searched:\n")) | ||
| res = jump_search(arr, x) | ||
| if res == -1: | ||
| print("Number not found!") | ||
| else: | ||
| print(f"Number {x} is at index {res}") | ||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.


First import List from typing