Skip to content

Commit 49d8b7f

Browse files
committed
Binary Search
1 parent 72c7466 commit 49d8b7f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ Data Structures and Algorithms Patterns implemented in Python.
1818
- [x] [Reverse Linked List](Recursion/Linked-List/Reverse-Linked-List.py)
1919
- [x] [Graphs](Graphs)
2020
- [x] [Breadth First Search](Graphs/bfs.py)
21-
- [x] [Depth First Search](Graphs/dfs.py)
21+
- [x] [Depth First Search](Graphs/dfs.py)
22+
- [x] [Searching Alogorithms](Searching-Algo)
23+
- [x] [Binary Search](Searching-Algo/binarysearch.py)
24+

Searching-Algo/binarysearch.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Binary Search
3+
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half.
4+
'''
5+
6+
def binarysearch(list,target):
7+
left=0
8+
right=len(list)-1
9+
while left <= right:
10+
mid = (left+right)//2
11+
if target == list[mid]:
12+
return mid
13+
elif target> list[mid]:
14+
left=mid+1
15+
else:
16+
right=mid-1
17+
18+
return -1
19+
20+
print(binarysearch([2,3,4,5],5)) #3

0 commit comments

Comments
 (0)