Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a480b80

Browse files
Merge pull request #35 from Umesh-01/rounaque-h-patch-5
Create BinarySearchIterative.py
2 parents 2df8005 + 508f8de commit a480b80

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎BinarySearchIterative.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def binarySearch(arr, l, r, x):
2+
3+
while l <= r:
4+
5+
mid = l + (r - l) // 2
6+
7+
# Check if x is present at mid
8+
if arr[mid] == x:
9+
return mid
10+
11+
# If x is greater, ignore left half
12+
elif arr[mid] < x:
13+
l = mid + 1
14+
15+
# If x is smaller, ignore right half
16+
else:
17+
r = mid - 1
18+
19+
# If we reach here, then the element
20+
# was not present
21+
return -1
22+
23+
24+
# Driver Code
25+
arr = [2, 3, 4, 10, 40]
26+
x = 10
27+
28+
# Function call
29+
result = binarySearch(arr, 0, len(arr)-1, x)
30+
31+
if result != -1:
32+
print("Element is present at index % d" % result)
33+
else:
34+
print("Element is not present in array")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /