Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize binary search by python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report