chore(Python): add doctest for jump_search (#255)
parent
0073fbd332
commit
56d42f2287
|
@ -10,8 +10,14 @@ Space Complexity: O(1)
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
arr = [0, 1, 2, 8, 13, 17, 19, 25, 31, 32, 42]
|
||||||
|
target = 25
|
||||||
|
|
||||||
def jump_search(arr: list, x: int) -> int:
|
def jump_search(arr: list, x: int) -> int:
|
||||||
|
"""
|
||||||
|
>>> jump_search(arr, target) == (arr.index(target) if target in arr else -1)
|
||||||
|
True
|
||||||
|
"""
|
||||||
n = len(arr)
|
n = len(arr)
|
||||||
step = int(math.floor(math.sqrt(n)))
|
step = int(math.floor(math.sqrt(n)))
|
||||||
prev = 0
|
prev = 0
|
||||||
|
@ -37,8 +43,6 @@ def check_sort(test: list) -> bool:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
arr = [0, 1, 2, 8, 13, 17, 19, 25, 31, 32, 42]
|
|
||||||
target = 25
|
|
||||||
if check_sort(arr):
|
if check_sort(arr):
|
||||||
res = jump_search(arr, target)
|
res = jump_search(arr, target)
|
||||||
if res == -1:
|
if res == -1:
|
||||||
|
@ -46,4 +50,4 @@ if __name__ == "__main__":
|
||||||
else:
|
else:
|
||||||
print(f"Number {target} is at index {res}")
|
print(f"Number {target} is at index {res}")
|
||||||
else:
|
else:
|
||||||
print("Given list is not sorted!")
|
print("Given list is not sorted!")
|
||||||
|
|
Loading…
Reference in New Issue