Skip to content

Commit

Permalink
Create Jump_search.py
Browse files Browse the repository at this point in the history
  • Loading branch information
idevpawan committed Oct 10, 2022
1 parent 702265a commit a5bfe29
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Jump_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610 ]
x = 55
n = len(arr)


index = jumpSearch(arr, x, n)

print("Number" , x, "is at index" ,"%.0f"%index)

0 comments on commit a5bfe29

Please sign in to comment.