# Jump Search

--

Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.

For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the element x.

Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the array is 16. Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4.

STEP 1: Jump from index 0 to index 4;

STEP 2: Jump from index 4 to index 8;

STEP 3: Jump from index 8 to index 12;

STEP 4: Since the element at index 12 is greater than 55 we will jump back a step to come to index 8.

STEP 5: Perform linear search from index 8 to get the element 55.

link:https://www.geeksforgeeks.org/jump-search/

code:

arr=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

element= 55def jump_search(arr,element):

index = 0

jump_by = int(len(arr)/4)

for _ in range(len(arr)):

index += jump_by

# if your index goes beyond the length of your array

if index >= len(arr)-1:

# go back to the previous index and do a linear search

index-= jump_by

for i in range(len(arr[index:])):

if arr[index] == element:

return index

index += 1

#if you can't find it in this last sub-array, return -1

return -1

#if your index has jumped over your element

if arr[index] >= element:

#go back to prev index and do a linear search

index -= jump_by

for i in range(len(arr[index:])):

if arr[index] == element:

return index

index += 1

# if you can't find it in the whole array, return -1

return -1

##############################

print(arr)

res = jump_search(arr,element)

if res > -1:

print("searching for = ", element, "\nelement was found to be at index",res)

else:

print(-1)