-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
37 lines (23 loc) · 842 Bytes
/
BinarySearch.py
File metadata and controls
37 lines (23 loc) · 842 Bytes
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
class Inter:
arr = [ ]
def __init__(self, arr, x):
self.arr = arr
self.x = x
def find(self, low, high):
if low <= high:
mid_index = ((low + high) // 2)
mid = self.arr[mid_index]
self.output = 'Not in list'
if self.x > mid:
self.find(mid_index + 1, high)
elif self.x < mid:
self.find(low, mid_index - 1)
elif self.x == mid:
self.output = mid_index
def __str__(self):
return f'element: {self.output}'
mass = [10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47]
print(*mass)
inter = Inter(mass, int(input('Find?\n->')))
inter.find(0, (len(mass) - 1))
print(inter)