Binary Search in Python
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:
https://youtube.com/playlist?list=PLnKe36F30Y4bcRomKi02sP9NR27KnBqCK
Github: https://github.com/ImKennyYip/python-data-structures-algorithms
Subscribe for more coding tutorials 😄!
Other Videos By Kenny Yip Coding
2024-02-19 | C++ Break and Continue Statements |
2024-02-14 | Do While Loops in C++ |
2024-02-12 | While Loops in C++ |
2024-02-08 | For Each Loop in C++ |
2024-01-30 | Code Flappy Bird in Java |
2024-01-29 | For Loops in C++ |
2024-01-18 | Why does index start at 0 in computer science? |
2024-01-17 | C++ Vectors and Dynamic Arrays |
2024-01-16 | C++ String and C Strings |
2024-01-08 | Arrays in C++ |
2023-11-02 | Binary Search in Python |
2023-10-30 | Space Complexity of Algorithms |
2023-10-29 | Linear Search in Python |
2023-10-26 | Two Sum with Sorted Array |
2023-10-13 | How to set up C++ in Visual Studio Code |
2023-10-09 | Code Snake Game in Python |
2023-09-19 | Code a game in Java |
2023-08-14 | Code Minesweeper in Java |
2023-08-02 | Code Black Jack in Java |
2023-07-19 | Code Snake Game in Java |
2023-07-16 | Code Tic Tac Toe in Java |