Browse Source

Added Jump Search

pull/19/head
Sahil Agrawal 5 years ago
committed by GitHub
parent
commit
8191cf97de
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 0 deletions
  1. +27
    -0
      searching_algo/jump_search,py

+ 27
- 0
searching_algo/jump_search,py View File

@ -0,0 +1,27 @@
'''
Sends array, searchValue, arraySize as input.
Returns the index of the searchValue in the array
'''
def jumpSearch(arr , x , n ):
# Finding block size to be jumped
step = math.sqrt(n)
# Finding the element
prev = 0
while arr[int(min(step, n)-1)] < x:
prev = step
step += math.sqrt(n)
if prev >= n:
return -1
# Doing a linear search
while arr[int(prev)] < x:
prev += 1
if prev == min(step, n):
return -1
if arr[int(prev)] == x:
return prev
return -1

Loading…
Cancel
Save