Featured
- Get link
- Other Apps
Simple implementation of binary search in Python
Simple implementation of binary search in Python
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjW1Oh1PQSaCsCMZRLUVYfyrUECLXbxSk0EEYiqfYRv-MO2hjdOI_-l79SwKxA6_OCOfdV-JUrGBD-Is77sJU4th1kw_LduAefUg0x_fCqXrxpmnmHNhY16m5JIWxr76gkUd0Y-8_lg90swQLF7vNDO4EL4OlwyKVI82DJNFuPFEzuIWsDO0l7FM4yyDLNm/w509-h231/images%20(1).png)
Certainly! Here's a simple implementation of binary search in Python:
```python
def binary_search(arr, target):
"""
Perform binary search on a sorted list.
Parameters:
- arr: A sorted list of elements.
- target: The element to search for.
Returns:
- index: The index of the target element if found, otherwise -1.
"""
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
mid_element = arr[mid]
if mid_element == target:
return mid # Target found, return its index
elif mid_element < target:
low = mid + 1 # Discard the left half
else:
high = mid - 1 # Discard the right half
return -1 # Target not found
# Example usage:
sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target_element = 7
result = binary_search(sorted_list, target_element)
if result != -1:
print(f"Element {target_element} found at index {result}.")
else:
print(f"Element {target_element} not found in the list.")
```
This function takes a sorted list (`arr`) and a target element (`target`) as input. It returns the index of the target element if found, otherwise, it returns -1. The binary search algorithm compares the target element with the middle element of the list and eliminates half of the remaining elements based on the comparison, repeating this process until the target is found or the search space is empty.
Certainly! Here's a simple implementation of binary search in Python:
```python
def binary_search(arr, target):
"""
Perform binary search on a sorted list.
Parameters:
- arr: A sorted list of elements.
- target: The element to search for.
Returns:
- index: The index of the target element if found, otherwise -1.
"""
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
mid_element = arr[mid]
if mid_element == target:
return mid # Target found, return its index
elif mid_element < target:
low = mid + 1 # Discard the left half
else:
high = mid - 1 # Discard the right half
return -1 # Target not found
# Example usage:
sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target_element = 7
result = binary_search(sorted_list, target_element)
if result != -1:
print(f"Element {target_element} found at index {result}.")
else:
print(f"Element {target_element} not found in the list.")
```
This function takes a sorted list (`arr`) and a target element (`target`) as input. It returns the index of the target element if found, otherwise, it returns -1. The binary search algorithm compares the target element with the middle element of the list and eliminates half of the remaining elements based on the comparison, repeating this process until the target is found or the search space is empty.
Certainly! Here's a simple implementation of binary search in Python:
```python
def binary_search(arr, target):
"""
Perform binary search on a sorted list.
Parameters:
- arr: A sorted list of elements.
- target: The element to search for.
Returns:
- index: The index of the target element if found, otherwise -1.
"""
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
mid_element = arr[mid]
if mid_element == target:
return mid # Target found, return its index
elif mid_element < target:
low = mid + 1 # Discard the left half
else:
high = mid - 1 # Discard the right half
return -1 # Target not found
# Example usage:
sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target_element = 7
result = binary_search(sorted_list, target_element)
if result != -1:
print(f"Element {target_element} found at index {result}.")
else:
print(f"Element {target_element} not found in the list.")
```
This function takes a sorted list (`arr`) and a target element (`target`) as input. It returns the index of the target element if found, otherwise, it returns -1. The binary search algorithm compares the target element with the middle element of the list and eliminates half of the remaining elements based on the comparison, repeating this process until the target is found or the search space is empty.
Comments
Post a Comment
NAME
EMAIL ID
MESSAGE