Skip to content

Commit 845d6c4

Browse files
Moving files
1 parent e6c908f commit 845d6c4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Arrays-searching/binary_search.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Written By @searpheon - Arka
3+
BINARY SEARCH
4+
"""
5+
6+
def binary_search(item_list,item): //creating the function
7+
first = 0 //first element
8+
last = len(item_list)-1 //self explanatory
9+
found = False //setting value top false if we find the element we are looking for
10+
while( first<=last and not found):
11+
mid = (first + last)//setting value of middle element
12+
if item_list[mid] == item :
13+
found = True
14+
else:
15+
if item < item_list[mid]:
16+
last = mid - 1 //shifting middle element's subset to the left
17+
else:
18+
first = mid + 1 //" " " " to the right
19+
return found
20+
21+
print(binary_search([1,2,3,5,8], 6))
22+
print(binary_search([1,2,3,5,8], 5))

0 commit comments

Comments
 (0)