-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Jump search #2415
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
Changes from 8 commits
1f99120
b5769f7
3708397
4cfb62f
86b62a9
28a56c8
fb2b5ba
2750f60
0ab3772
25259cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
""" | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tend to like to raise an exception like str.index() vs str.find() but that is a personal choice. |
||
>>> 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,7 @@ 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 | ||
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")) | ||
print(f"Number {x} is at index {jump_search(arr, x)}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 is a valid position in python, check if the number is found before printing it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First import List from typing
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This limits us to only the int data type. Without this specificity, would the algorithm work with strings, tuples, floats?