From 8191cf97de6d84cdf5ecd3c4e972c68ebfdb5e04 Mon Sep 17 00:00:00 2001 From: Sahil Agrawal Date: Sat, 19 Oct 2019 17:16:47 +0530 Subject: [PATCH] Added Jump Search --- searching_algo/jump_search,py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 searching_algo/jump_search,py diff --git a/searching_algo/jump_search,py b/searching_algo/jump_search,py new file mode 100644 index 0000000..608b742 --- /dev/null +++ b/searching_algo/jump_search,py @@ -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