Binary Search in Python

Опубликовано: 15 Февраль 2025
на канале: Kenny Yip Coding
685
9

What is binary search and how to implement binary search in python. Binary search is a searching algorithm used to find a value in a sorted sequence. If the list is sorted, we can partition the list into 2 sublists using a midpoint index.

If the value at the midpoint is smaller than the target number, we can discard the left sublist and just check the right sublist. Else, if the value is bigger, then we can discard the right sublist and check the left subilst. This algorithm works because the list is sorted so you are guaranteed to avoid doing extra comparisons.

For this reason, the binary search algorithm has a run-time of O(log(n)), which is the number of times n can be divided by 2. This is more efficient than the linear search O(n) as we can quickly eliminate values in the list.

Python Playlist:
   • Python Data Structures and Algorithms  

Github: https://github.com/ImKennyYip/python-...

Subscribe for more coding tutorials 😄!