From 59c148fe7e69a45a3438b9627e9302d23b70a495 Mon Sep 17 00:00:00 2001 From: Baigel <43929690+Baigel@users.noreply.github.com> Date: Fri, 25 Oct 2019 10:43:25 +1000 Subject: [PATCH] Create Jump_Search.py --- SearchingAlgorithms/Jump_Search.py | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 SearchingAlgorithms/Jump_Search.py diff --git a/SearchingAlgorithms/Jump_Search.py b/SearchingAlgorithms/Jump_Search.py new file mode 100644 index 0000000..787420a --- /dev/null +++ b/SearchingAlgorithms/Jump_Search.py @@ -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))