Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Create Jump_Search.py #126

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
31 changes: 31 additions & 0 deletions SearchingAlgorithms/Jump_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
""" Python3 Jump Search for Sorted Arrays """

import math

def jumpSearch(arr, x):
# Define Block Size
step = int(math.sqrt(len(arr)))

# Finding the block where element is
# present (if it is present)
block = 0
while arr[min(step*block, len(arr))] < x:
block += 1
if step*(block+1) > len(arr):
break

# Linear search for x
linear = (block-1)*step
while arr[linear] != x and linear < len(arr)-1:
linear += 1
if linear > block * step:
return -1

# Check to see it is value, and not just end position
if arr[linear] == x:
return linear
return -1


# Example
print(jumpSearch([0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610],22))