Binary Search is a technique used to search element in a sorted list. In this article, we will looking at library functions to do Binary Search.
Finding first occurrence of an element.
bisect.bisect_left(a, x, lo=0, hi=len(a)) : Returns leftmost insertion point of x in a sorted list. Last two parameters are optional, they are used to search in sublist.
First occurrence of 4 is present at 2
Finding greatest value smaller than x.
Largest value smaller than 7 is at index 3
Finding rightmost occurrence
bisect.bisect_right(a, x, lo=0, hi=len(a)) Returns rightmost insertion point of x in a sorted list a. Last two parameters are optional, they are used to search in sublist.
Last occurrence of 4 is present at 3
Please refer Binary Search for writing your own Binary Search code.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Bisect Algorithm Functions in Python
- Meta Binary Search | One-Sided Binary Search
- Python Program for Binary Search (Recursive and Iterative)
- Why is Binary Search preferred over Ternary Search?
- Interpolation search vs Binary search
- Linear Search vs Binary Search
- Python Program for Anagram Substring Search (Or Search for all permutations)
- Binary Search in PHP
- Binary Search
- Uniform Binary Search
- Binary Search in Java
- The Ubiquitous Binary Search | Set 1
- Variants of Binary Search
- Binary Search using pthread
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.