From f8d9e067b996fbfd9d0ce9ea4a9fe093271a8853 Mon Sep 17 00:00:00 2001 From: Gopikrishnan K <43175555+SuperGops7@users.noreply.github.com> Date: Thu, 31 Oct 2019 21:11:47 +0530 Subject: [PATCH] Create jump_search --- sorting and basics/jump_search | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 sorting and basics/jump_search diff --git a/sorting and basics/jump_search b/sorting and basics/jump_search new file mode 100644 index 0000000..f400883 --- /dev/null +++ b/sorting and basics/jump_search @@ -0,0 +1,19 @@ +import math + +def jmpFind( 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 #element not found + if arr[int(prev)] == x: + return prev + + return -1