This repository contains a Python implementation of the Binary Search algorithm. Binary Search is a classic algorithm used to efficiently find a target value within a sorted sequence or array. It works by repeatedly dividing in half the portion of the list that could contain the target value until it isolates the value or determines that it's not present.
Binary Search is preferred for sorted arrays due to its time complexity of O(log n), making it significantly faster than linear search algorithms for large datasets. This implementation assumes the input array (arr
) is sorted in ascending order.
The binary_search
function takes two parameters:
arr
: A sorted list of elements where the target value will be searched.target
: The value to be searched within thearr
.
The function returns:
- The index of
target
inarr
iftarget
is present. -1
iftarget
is not found inarr
.
Here is the Python code for the binary_search
function:
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
You can use the binary_search
function as follows:
arr = [2, 4, 6, 8, 10, 12, 14, 16]
target = 16
result = binary_search(arr, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
In this example:
arr
is a sorted list of integers.target
is the value we want to find withinarr
.binary_search(arr, target)
returns the index oftarget
if found, or-1
if not found. .py
Feel free to explore, modify, and integrate this Binary Search implementation into your own projects. If you have any questions, suggestions, or improvements, please feel free to open an issue or pull request. Your feedback is highly appreciated!
This project is licensed under the MIT License - see the LICENSE file for details.