kopia lustrzana https://github.com/animator/learn-python
Update Binary search.py
rodzic
38d6251b2b
commit
efc6b63ec8
|
@ -1,30 +1,31 @@
|
||||||
def binarySearch(arr, low, high, x):
|
|
||||||
|
|
||||||
if high >= low:
|
def binary_search(arr, target):
|
||||||
|
low = 0
|
||||||
|
high = len(arr) - 1
|
||||||
|
|
||||||
mid = low + (high - low) // 2
|
while low <= high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
mid_val = arr[mid]
|
||||||
|
|
||||||
if arr[mid] == x:
|
if mid_val == target:
|
||||||
return mid
|
return mid
|
||||||
|
elif mid_val < target:
|
||||||
elif arr[mid] > x:
|
low = mid + 1
|
||||||
return binarySearch(arr, low, mid-1, x)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return binarySearch(arr, mid + 1, high, x)
|
high = mid - 1
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def main():
|
||||||
|
arr = list(map(int, input("Enter the array elements: ").split()))
|
||||||
|
arr.sort()
|
||||||
|
target = int(input("Enter the number to search for: "))
|
||||||
|
|
||||||
|
index = binary_search(arr, target)
|
||||||
|
if index != -1:
|
||||||
|
print(f"{target} found at index {index}.")
|
||||||
else:
|
else:
|
||||||
return -1
|
print(f"{target} not found.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
if __name__ == '__main__':
|
main()
|
||||||
arr = list(map(int,input("Enter the array elements: ").split()))
|
|
||||||
x = int(input("enter the number to search: "))
|
|
||||||
|
|
||||||
# Function call
|
|
||||||
result = binarySearch(arr, 0, len(arr)-1, x)
|
|
||||||
|
|
||||||
if result != -1:
|
|
||||||
print("Element is present at index", result)
|
|
||||||
else:
|
|
||||||
print("Element is not present in array")
|
|
||||||
|
|
Ładowanie…
Reference in New Issue