2 parents 4306482 + 09131a7 commit 7f87515Copy full SHA for 7f87515
1 file changed
searches/jump_search.py
@@ -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
16
+ if arr[prev] == x:
17
+ return prev
18
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