From a2c53791c0fd81d6125d7fc5280d52c4d5b8ca21 Mon Sep 17 00:00:00 2001 From: Sejal Rai Date: Sun, 17 Oct 2021 18:00:52 +0530 Subject: [PATCH] added jump search.py --- Data Structure/jump-search.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Data Structure/jump-search.py diff --git a/Data Structure/jump-search.py b/Data Structure/jump-search.py new file mode 100644 index 0000000..a99f00f --- /dev/null +++ b/Data Structure/jump-search.py @@ -0,0 +1,30 @@ +import math +def jumpSearch( arr , x , n ): + step = math.sqrt(n) + prev = 0 + while arr[int(min(step, n)-1)] < x: + prev = step + step += math.sqrt(n) + if prev >= n: + return -1 + while arr[int(prev)] < x: + prev += 1 + if prev == min(step, n): + return -1 + if arr[int(prev)] == x: + return prev + + return -1 + +arr = [ 0, 1, 1, 2, 3, 3, 8, 13, 21, + 34, 55, 89, 144, 233, 377, 610 ] +x = 5 +n = len(arr) + + +index = jumpSearch(arr, x, n) + +if(int(index)==-1): + print("Number" , x, "not found in the array"); +else: + print("Number" , x, "is at index" ,int(index))