kopia lustrzana https://github.com/animator/learn-python
Update Jump Search.py
rodzic
efc6b63ec8
commit
7b2e048bb7
|
@ -1,34 +1,33 @@
|
||||||
|
arr = list(map(int, input("Enter the array elements : ").split()))
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
def jumpSearch( arr , x , n ):
|
def jump_search(arr, target):
|
||||||
|
n = len(arr)
|
||||||
step = math.sqrt(n)
|
arr.sort() # Ensure array is sorted
|
||||||
|
step = int(math.sqrt(n))
|
||||||
|
|
||||||
prev = 0
|
prev = 0
|
||||||
while arr[int(min(step, n)-1)] < x:
|
|
||||||
|
while arr[min(step, n)-1] < target:
|
||||||
prev = step
|
prev = step
|
||||||
step += math.sqrt(n)
|
step += int(math.sqrt(n))
|
||||||
if prev >= n:
|
if prev >= n:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
while arr[prev] < target:
|
||||||
while arr[int(prev)] < x:
|
|
||||||
prev += 1
|
prev += 1
|
||||||
|
|
||||||
|
|
||||||
if prev == min(step, n):
|
if prev == min(step, n):
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
if arr[int(prev)] == x:
|
if arr[prev] == target:
|
||||||
return prev
|
return prev
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
arr = list(map(int,input("Enter the array elements: ").split()))
|
target = int(input("Enter the number to search for: "))
|
||||||
x = int(input("enter the number to search: "))
|
|
||||||
n = len(arr)
|
|
||||||
|
|
||||||
index = jumpSearch(arr, x, n)
|
index = jump_search(arr, target)
|
||||||
|
if index != -1:
|
||||||
print("Number" , x, "is at index" ,"%.0f"%index)
|
print(f"{target} found at index {index}.")
|
||||||
|
else:
|
||||||
|
print(f"{target} not found in the array.")
|
||||||
|
|
Ładowanie…
Reference in New Issue