Skip to content

Commit 09131a7

Browse files
implemented jump search
1 parent f9156cf commit 09131a7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: searches/jump_search.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
def jump_search(arr, x):
3+
n = len(arr)
4+
step = math.floor(math.sqrt(n))
5+
prev = 0
6+
while arr[min(step, n)-1] < x:
7+
prev = step
8+
step += math.floor(math.sqrt(n))
9+
if prev >= n:
10+
return -1
11+
12+
while arr[prev] < x:
13+
prev = prev + 1
14+
if prev == min(step, n):
15+
return -1
16+
if arr[prev] == x:
17+
return prev
18+
return -1
19+
20+
21+
22+
arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
23+
x = 55
24+
index = jump_search(arr, x)
25+
print("\nNumber " + str(x) +" is at index " + str(index));

0 commit comments

Comments
 (0)