How to realize binary search by python
This article mainly introduces python how to achieve binary search, the article is very detailed, has a certain reference value, interested friends must read it!
1. Take the element e of the intermediate value mid into the series and compare the lookup element key.
2. If the equality search is successful, if it is unequal, if it is greater than it, you only need to find it in the second half, and if it is less than that, you need to find it in the first half.
Example
Def binary_search (my_list, key): left = 0 right = len (my_list) while left key: right = left + mid-1 else: return left + mid return "None" if _ name__ = = "_ main__": my_list = [1, 3, 5, 7, 9, 11, 13] print ("original sequence for binary search:" My_list) print ("return results of binary search:", binary_search (my_list, 3)) are all the contents of the article "how to achieve binary search by python" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!