Skip to content

Commit

Permalink
implemented jump search
Browse files Browse the repository at this point in the history
  • Loading branch information
senapati-deepak committed Oct 12, 2017
1 parent f9156cf commit 09131a7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions searches/jump_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import math
def jump_search(arr, x):
n = len(arr)
step = math.floor(math.sqrt(n))
prev = 0
while arr[min(step, n)-1] < x:
prev = step
step += math.floor(math.sqrt(n))
if prev >= n:
return -1

while arr[prev] < x:
prev = prev + 1
if prev == min(step, n):
return -1
if arr[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
index = jump_search(arr, x)
print("\nNumber " + str(x) +" is at index " + str(index));

0 comments on commit 09131a7

Please sign in to comment.