learn-python/Searching algorithms/Jump Search.py

34 wiersze
725 B
Python
Czysty Zwykły widok Historia

2024-05-16 12:00:14 +00:00
arr = list(map(int, input("Enter the array elements : ").split()))
2024-05-16 11:46:07 +00:00
import math
2024-05-16 12:00:14 +00:00
def jump_search(arr, target):
n = len(arr)
arr.sort() # Ensure array is sorted
step = int(math.sqrt(n))
prev = 0
while arr[min(step, n)-1] < target:
prev = step
step += int(math.sqrt(n))
if prev >= n:
return -1
while arr[prev] < target:
prev += 1
if prev == min(step, n):
return -1
if arr[prev] == target:
return prev
return -1
target = int(input("Enter the number to search for: "))
index = jump_search(arr, target)
if index != -1:
print(f"{target} found at index {index}.")
else:
print(f"{target} not found in the array.")