-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBS.py
More file actions
57 lines (40 loc) · 1.26 KB
/
Copy pathBS.py
File metadata and controls
57 lines (40 loc) · 1.26 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
# Python 3 program for recursive binary search.
# Modifications needed for the older Python 2 are found in comments.
# Returns index of x in arr if present, else -1
numbers = [1, 5, 6, 9, 23, 45]
x = 1
def linear_search_one(numbers, x):
for i in range(len(numbers)):
if numbers[i] == x:
print("Element Found")
return x
def binary_search(numbers, low, high, x):
if high >= low:
mid = (low + high) // 2
if numbers[mid] == x:
print("Number found")
elif numbers[mid] > x:
binary_search(numbers, low, mid - 1, x)
else:
binary_search(numbers, mid + 1, high, x)
else:
return -1
def linear_search(numbers, x):
for i in range(len(numbers)):
if numbers[i] == x:
print("ELement found")
return x
print("Element not founf")
def binary_search(numbers, low, high, x):
if high >= low:
mid = (low + high) // 2
if numbers[mid] == x:
print(x)
elif numbers[mid] > x:
binary_search(numbers, low, mid - 1, x)
else:
binary_search(numbers, mid + 1, high, x)
else:
return -1
# binary_search(numbers, 0, len(numbers) - 1, x)
linear_search(numbers, 5)