Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Searching/Python/Jump_search.py
Original file line number Diff line number Diff line change
@@ -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")
18 changes: 18 additions & 0 deletions Searching/Python/Linear_search.py
Original file line number Diff line number Diff line change
@@ -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")