-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary Searching.py
49 lines (48 loc) · 1.42 KB
/
Binary Searching.py
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
# Binary Searching- 1
'''
def binary_search(L,i):
left, right = 0, len(L)-1
while left <= right:
mid = (left + right)//2 # integer divison
if L[mid] == i:
return mid
if L[mid] < i:
left = mid + 1
else:
right = mid -1
return -1
if __name__ == "__main__":
L = [1,2,3,4,5,6,7,8]
for i in range(1,11):
position = binary_search(L,i)
if position == -1:
if i in L:
print(i, "is in L, But function freturned -1")
else:
print(i, "not in list")
else:
if L[position] == i:
print(i, "found in correct position.")
else:
print("Binary search returned", position, "for", i,"which is incorrect")
print("Program terminated")
'''
#Binary Searching - 2
L = list(map(int,input().split()))
print("Main List: ",L)
N = int(input())
left, right = 0, len(L) - 1
print("Total Length: ",right)
while left <= right:
mid = (left + right)//2
if L[mid] == N:
print("Baniry Position: ",mid + 1)
break
elif L[mid] < N:
left = mid + 1
print("left position : ",left)
else:
right = mid - 1
print("right position: ",right)
else:
print("Sorry! %d Not found.: ",N)