Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Jump search #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pygorithm/searching/JumpSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#This code is contributed by Sakshi-Mallick

import math

def jumpSearch( a , x , n ):

# Finding block size to be jumped
step = math.sqrt(n)

# Finding the block where element is
# present (if it is present)
pre = 0
while ar[int(min(step, n)-1)] < x:
pre = step
step += math.sqrt(n)
if pre >= n:
return -1

# Doing a linear search for x in
# block beginning with prev.
while a[int(prev)] < x:
pre += 1

# If we reached next block or end
# of array, element is not present.
if pre == min(step, n):
return -1

# If element is found
if a[int(pre)] == x:
return pre

return -1

# Driver code to test function
a = [ 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610 ]
x = 55
n = len(a)

# Find the index of 'x' using Jump Search
index = jumpSearch(arr, x, n)

# Print the index where 'x' is located
print("Number" , x, "is at index" ,"%.0f"%index)