Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ishitagupta8720 authored Oct 20, 2019
1 parent 655d1df commit d8f62e6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
def binarySearch(arr, l, r, x):
while l <= r:

mid = l + (r - l) / 2;
mid = l + (r - l) / 2; #extracting the middle element of the array

# Check if x is present at mid
if arr[mid] == x:
return mid

# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1
l = mid + 1 #l is initialised to the rightmost element of the middle so that the search could be started from there the next time

# If x is smaller, ignore right half
else:
r = mid - 1
r = mid - 1 #r is initialised to the leftmost element of the middle so that the search goes till there only the next time

# If we reach here, then the element was not present
return -1
Expand All @@ -25,12 +25,14 @@ def binarySearch(arr, l, r, x):
if __name__ == "__main__":
# User input array
print("Enter the array with comma separated in which element will be searched")
arr = map(int, input().split(","))
arr = map(int, input().split(",")) #the input array will of int type with each element seperated with a comma due to the split fucntion
#map function returns a list of results after applying the given function to each item
x = int(input("Enter the element you want to search in given array"))

# Function call
result = binarySearch(arr, 0, len(arr) - 1, x)


#printing the output
if result != -1:
print("Element is present at index {}".format(result))
else:
Expand Down

0 comments on commit d8f62e6

Please sign in to comment.