An open API service indexing awesome lists of open source software.

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

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)
```