From 30e5b59bc0d6f619cb6f25c513fe49bb7721100d Mon Sep 17 00:00:00 2001 From: Kavin18 <148603725+Kavin56@users.noreply.github.com> Date: Thu, 16 May 2024 17:16:07 +0530 Subject: [PATCH] Create Jump Search.py --- Searching algorithms/Jump Search.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Searching algorithms/Jump Search.py diff --git a/Searching algorithms/Jump Search.py b/Searching algorithms/Jump Search.py new file mode 100644 index 0000000..9bcc4f0 --- /dev/null +++ b/Searching algorithms/Jump Search.py @@ -0,0 +1,34 @@ +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 = list(map(int,input("Enter the array elements: ").split())) +x = int(input("enter the number to search: ")) +n = len(arr) + +index = jumpSearch(arr, x, n) + +print("Number" , x, "is at index" ,"%.0f"%index)