From 78cd0e16020366b7d36a3097cfdc258920a36a94 Mon Sep 17 00:00:00 2001 From: Saeed Date: Wed, 2 Oct 2019 00:11:54 +0330 Subject: [PATCH] The Jump Search --- searches/jump_search.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 searches/jump_search.py diff --git a/searches/jump_search.py b/searches/jump_search.py new file mode 100644 index 0000000..b34efc6 --- /dev/null +++ b/searches/jump_search.py @@ -0,0 +1,20 @@ +import math + +def jumoSearch (listA, theGoalValue): + length = len(listA) + jump = int(math.sqrt(length)) + left, right = 0, 0 + while length > left && theGoalValue >= listA[left]: + right = min(length - 1, left + jump) + if listA[left] <= theGoalValue and listA[right] >= theGoalValue: + break + left += jump; + if left >= length or listA[left] > theGoalValue: + return -1 + right = min(length - 1, right) + i = left + while i <= right and listA[i] <= theGoalValue: + if listA[i] == theGoalValue: + return i + i += 1 + return -1