https://github.com/amssdias/python-binary-search-algorithm
Binary Search Algorithm
https://github.com/amssdias/python-binary-search-algorithm
algorithm binary-search
Last synced: 5 months ago
JSON representation
Binary Search Algorithm
- Host: GitHub
- URL: https://github.com/amssdias/python-binary-search-algorithm
- Owner: amssdias
- Created: 2021-04-08T13:19:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-08T13:22:08.000Z (over 4 years ago)
- Last Synced: 2025-07-09T04:05:56.910Z (5 months ago)
- Topics: algorithm, binary-search
- Language: Python
- Homepage:
- Size: 1000 Bytes
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Binary Search in Python
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.
## My solution
```python
from math import ceil
def binary_search(array, item):
if len(array) == 0:
return "No items in the list"
half = ceil(len(array) / 2) - 1
if array[half] == item:
return f"Found item: {item}"
elif item > array[half]:
new_list = array[half + 1:]
return binary_search(new_list, item)
elif item < array[half]:
new_list = array[:half]
return binary_search(new_list, item)
```