Update Jump Search.py

pull/343/head
Kavin18 2024-05-16 17:30:14 +05:30 zatwierdzone przez GitHub
rodzic efc6b63ec8
commit 7b2e048bb7
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 25 dodań i 26 usunięć

Wyświetl plik

@ -1,34 +1,33 @@
arr = list(map(int, input("Enter the array elements : ").split()))
import math
def jumpSearch( arr , x , n ):
step = math.sqrt(n)
def jump_search(arr, target):
n = len(arr)
arr.sort() # Ensure array is sorted
step = int(math.sqrt(n))
prev = 0
prev = 0
while arr[int(min(step, n)-1)] < x:
prev = step
step += math.sqrt(n)
if prev >= n:
return -1
while arr[min(step, n)-1] < target:
prev = step
step += int(math.sqrt(n))
if prev >= n:
return -1
while arr[int(prev)] < x:
prev += 1
while arr[prev] < target:
prev += 1
if prev == min(step, n):
return -1
if prev == min(step, n):
return -1
if arr[int(prev)] == x:
return prev
return -1
if arr[prev] == target:
return prev
arr = list(map(int,input("Enter the array elements: ").split()))
x = int(input("enter the number to search: "))
n = len(arr)
return -1
index = jumpSearch(arr, x, n)
target = int(input("Enter the number to search for: "))
print("Number" , x, "is at index" ,"%.0f"%index)
index = jump_search(arr, target)
if index != -1:
print(f"{target} found at index {index}.")
else:
print(f"{target} not found in the array.")