-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Searching.py
More file actions
38 lines (33 loc) · 888 Bytes
/
Binary Searching.py
File metadata and controls
38 lines (33 loc) · 888 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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 17 21:27:23 2021
@author: DELL
"""
list_numbers=[]
try:
list_numbers=[]
while True:
list_numbers.append(int(input()))
# if the input is not-integer, just print the list
except:
print("List : " ,list_numbers)
searched_number=int(input())
first_pos=0
last_pos=len(list_numbers)-1
flag=0
count=1
list_numbers.sort()
while(first_pos<=last_pos and flag==0):
count+=1
mid=(first_pos+last_pos)//2
if searched_number==list_numbers[mid]:
print('The element is present at pos: '+str(mid))
print("The number of iterations are: "+str(count))
flag=1
else:
if(searched_number<list_numbers[mid]):
last_pos=mid-1
else:
first_pos=mid+1
if flag==0:
print("The number is not present.")