FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_practice/binary_search.py at master · danglingmind/python_practice · GitHub
danglingmind
python_practice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python_practice
/
binary_search.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (21 loc) · 648 Bytes
Breadcrumbs
python_practice
/
binary_search.py
Copy path
File metadata and controls
26 lines (21 loc) · 648 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
def
binary_search
(
arr
:
list
,
ele
:
int
):
l
=
len
(
arr
)
if
l
>=
1
:
left
=
0
right
=
l
-
1
mid
=
int
((
left
+
right
)
/
2
)
mid_ele
=
arr
[
mid
]
if
ele
==
mid_ele
:
print
(
'found'
)
elif
ele
<
mid_ele
:
binary_search
(
arr
[:
mid
],
ele
)
elif
ele
>
mid_ele
:
binary_search
(
arr
[
mid
+
1
:],
ele
)
else
:
print
(
'not found'
)
if
__name__
==
'__main__'
:
arr
=
list
(
map
(
int
,
input
(
'Enter space separated number : '
).
strip
().
split
()))
ele
=
int
(
input
(
'What to search : '
))
# binary search needs sorted array
arr
.
sort
()
binary_search
(
arr
,
ele
)
Back
|
FazBrowse Home
|
New Git URL