Skip to content

Commit 435de9c

Browse files
committed
Searching
1 parent ff5543b commit 435de9c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

searching/binarysearchiterative.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def binarysearch_iterative(A, key):
2+
l = 0
3+
r = len(A)-1
4+
while l <= r:
5+
mid = (l + r) // 2
6+
if key == A[mid]:
7+
return mid
8+
elif key < A[mid]:
9+
r = mid - 1
10+
elif key > A[mid]:
11+
l = mid + 1
12+
return -1
13+
14+
A = [15,21,47,84,96]
15+
found = binarysearch_iterative(A,84)
16+
print('Result: ',found)

0 commit comments

Comments
 (0)