-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.py
More file actions
52 lines (31 loc) · 951 Bytes
/
Copy pathbinarySearch.py
File metadata and controls
52 lines (31 loc) · 951 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
44
45
46
47
48
49
50
51
52
#in this video we ll implement binary search. i hope your concept is clear.
def binarySearch(list, num):
first = 0
last = len(list)-1
flag = 0
while first <= last :
mid = (first + last) // 2
if list[mid] == num:
flag =1
else:
if num < list[mid]:
last = mid -1
else:
first = mid + 1
return flag
#driver code
n = int(input("enter the size of the list: "))
list = []
for i in range(0, n):
p =int(input("enter the element: "))
list.append(p)
#we need a sorted list for binary search so lets use sort() function
number = int(input("enter the number to be searched: "))
list.sort()
print("\n",list,"\n")
result = binarySearch(list,number)
if result == 0:
print(number," not found the list")
else:
print(number,"is found in the list")
#thank you for watching