Create Jump Search.py

pull/343/head
Kavin18 2024-05-16 17:16:07 +05:30 zatwierdzone przez GitHub
rodzic 4630c00997
commit 30e5b59bc0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 34 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,34 @@
import math
def jumpSearch( arr , x , n ):
step = math.sqrt(n)
prev = 0
while arr[int(min(step, n)-1)] < x:
prev = step
step += math.sqrt(n)
if prev >= n:
return -1
while arr[int(prev)] < x:
prev += 1
if prev == min(step, n):
return -1
if arr[int(prev)] == x:
return prev
return -1
arr = list(map(int,input("Enter the array elements: ").split()))
x = int(input("enter the number to search: "))
n = len(arr)
index = jumpSearch(arr, x, n)
print("Number" , x, "is at index" ,"%.0f"%index)