I have implemented Jump search in Python, here is my code. Am I using the algorithm correctly ? Is there any pythonic way to achieve same ?
import math
def jump_search_2(arr,search):
interval = int(math.sqrt(len(arr)))
''' find last lowest element '''
for i in range(0,len(arr),interval):
if arr[i] < search:
low = i
elif arr[i] == search:
return i
else:
break
''' apply linear search '''
l_index = [e for e,i in enumerate(arr[low:low+interval]) if i == search]
if l_index[0]:
return low+l_index[0]
return "Not Found"
Usage example:
arr = [ i for i in range(1,300,15)]
res = jump_search_2(arr, 16)
print(res)
1at then end of the file. \$\endgroup\$1was the output \$\endgroup\$