-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
43 lines (36 loc) · 901 Bytes
/
BinarySearch.py
File metadata and controls
43 lines (36 loc) · 901 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
38
39
40
41
42
43
#Q. 83:
#Binary Search(Searching and Sorting)
#Write a program to implement binary search algorithm
def binarySearch (arr, l, r, x):
if r >= l:
mid = l + (r - l) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
else:
return binarySearch(arr, mid + 1, r, x)
else:
return -1
L = int(input())
arr = []
for i in range(L):
arr.append(int(input()))
S = int(input())
ele = []
for i in range(S):
ele.append(int(input()))
result = []
for i in range(S):
result.append(binarySearch(arr, 0, len(arr)-1, ele[i]))
f = 0
for i in range(S):
if result[i] == -1:
f = 0
break
else:
f = 1
if f == 0:
print("Not found")
else:
print("Sequence found between index " + str(result[0]) + " and " + str(result[-1]+1))