forked from arya2004/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchingAlgorithms.py
More file actions
127 lines (106 loc) · 3.17 KB
/
Copy pathSearchingAlgorithms.py
File metadata and controls
127 lines (106 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Searching Algorithms Implementation in Python
# 1️⃣ Linear Search
def linear_search(arr, target):
"""
Returns the index of the target if found, else -1.
Time Complexity: O(n)
"""
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
# 2️⃣ Binary Search (Iterative)
def binary_search_iterative(arr, target):
"""
Works only on sorted arrays.
Time Complexity: O(log n)
"""
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
# 3️⃣ Binary Search (Recursive)
def binary_search_recursive(arr, target, low=0, high=None):
"""
Recursive version of binary search.
Time Complexity: O(log n)
"""
if high is None:
high = len(arr) - 1
if low > high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return binary_search_recursive(arr, target, mid + 1, high)
else:
return binary_search_recursive(arr, target, low, mid - 1)
# 4️⃣ Jump Search
import math
def jump_search(arr, target):
"""
Works only on sorted arrays.
Time Complexity: O(√n)
"""
n = len(arr)
step = int(math.sqrt(n))
prev = 0
while prev < n and arr[min(step, n) - 1] < target:
prev = step
step += int(math.sqrt(n))
if prev >= n:
return -1
while prev < n and arr[prev] < target:
prev += 1
if prev == min(step, n):
return -1
if prev < n and arr[prev] == target:
return prev
return -1
# 5️⃣ Interpolation Search
def interpolation_search(arr, target):
"""
Works well for uniformly distributed sorted arrays.
Time Complexity: O(log log n) on average.
"""
low, high = 0, len(arr) - 1
while low <= high and target >= arr[low] and target <= arr[high]:
if low == high:
return low if arr[low] == target else -1
pos = low + ((high - low) * (target - arr[low])) // (arr[high] - arr[low])
if arr[pos] == target:
return pos
elif arr[pos] < target:
low = pos + 1
else:
high = pos - 1
return -1
# 6️⃣ Exponential Search
def exponential_search(arr, target):
"""
Works only on sorted arrays.
Time Complexity: O(log n)
"""
if arr[0] == target:
return 0
i = 1
while i < len(arr) and arr[i] <= target:
i *= 2
return binary_search_recursive(arr, target, i // 2, min(i, len(arr) - 1))
# 🧪 Example Run
if __name__ == "__main__":
arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 13
print("Linear Search:", linear_search(arr, target))
print("Binary Search (Iterative):", binary_search_iterative(arr, target))
print("Binary Search (Recursive):", binary_search_recursive(arr, target))
print("Jump Search:", jump_search(arr, target))
print("Interpolation Search:", interpolation_search(arr, target))
print("Exponential Search:", exponential_search(arr, target))