Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Data Structure/jump-search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 = [ 0, 1, 1, 2, 3, 3, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610 ]
x = 5
n = len(arr)


index = jumpSearch(arr, x, n)

if(int(index)==-1):
print("Number" , x, "not found in the array");
else:
print("Number" , x, "is at index" ,int(index))