diff --git a/Searching/Python/Jump_search.py b/Searching/Python/Jump_search.py new file mode 100644 index 0000000..a08511f --- /dev/null +++ b/Searching/Python/Jump_search.py @@ -0,0 +1,35 @@ +import math + +# Jump Search Function +def jump_search(arr, x): + length = len(arr) + step = int(math.sqrt(length)) + prev = 0 + + while arr[min(step, length) - 1] < x: + prev = step + step += int(math.sqrt(length)) + if prev >= length: + return -1 # Element not found + + # Perform linear search in the block + while arr[prev] < x: + prev += 1 + if prev == min(step, length): + return -1 # Element not found + + if arr[prev] == x: + return prev + return -1 + +# Data +arr = [2, 5, 6, 7, 8] +x = 6 + +# Function call +result = jump_search(arr, x) + +if result != -1: + print("Element is present at index", str(result)) +else: + print("Element is not present in array") diff --git a/Searching/Python/Linear_search.py b/Searching/Python/Linear_search.py new file mode 100644 index 0000000..142097c --- /dev/null +++ b/Searching/Python/Linear_search.py @@ -0,0 +1,18 @@ +# Linear Search Function +def linear_search(arr, x): + for index, value in enumerate(arr): + if value == x: + return index # Element found + return -1 # Element not found + +# Data +arr = [5, 8, 6, 7, 2] +x = 6 + +# Function call +result = linear_search(arr, x) + +if result != -1: + print("Element is present at index", str(result)) +else: + print("Element is not present in array")