Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize binary search by python

Shulou Source: shulou.com Published: 2022-06-03 13:49:43 09月11日 Update

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!

Tags: Elements content series articles original success value interest first half just need examples guys buddies more marks knowledge results industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker NVidia Shulou Technology Huawei