Skip to content

Commit 74b747b

Browse files
committed
algo: Added Jump Search
1 parent 91e1706 commit 74b747b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Searching Techniques/Jump_Search.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Jump Search is an efficient searching algorithm for ordered lists. It divides the list into smaller blocks and jumps ahead by fixed steps to quickly reach a block that might contain the target value. It then performs a linear search within that block, providing a balance between the efficiency of binary search and the simplicity of linear search. Jump Search is especially useful for large datasets where binary search might be less efficient due to its logarithmic time complexity.
2+
3+
import math
4+
5+
def jumpSearch(arr, x, n):
6+
7+
# Finding block size to be jumped
8+
step = math.sqrt(n)
9+
10+
# Finding the block where element is
11+
# present (if it is present)
12+
prev = 0
13+
while arr[int(min(step, n)-1)] < x:
14+
prev = step
15+
step += math.sqrt(n)
16+
if prev >= n:
17+
return -1
18+
19+
# Doing a linear search for x in
20+
# block beginning with prev.
21+
while arr[int(prev)] < x:
22+
prev += 1
23+
24+
# If we reached next block or end
25+
# of array, element is not present.
26+
if prev == min(step, n):
27+
return -1
28+
29+
# If element is found
30+
if arr[int(prev)] == x:
31+
return prev
32+
33+
return -1

0 commit comments

Comments
 (0)