Python Binary Search Algorithm instance analysis and python Binary Search

Source: Internet
Author: User

Python Binary Search Algorithm instance analysis and python Binary Search

This article analyzes the Python binary search algorithm. Share it with you for your reference. The specific analysis is as follows:

Today, when I read a book, the book mentioned that although the theory of the Bipartite method is simple, it is quite difficult for everyone to understand it when they hear it, even if you have enough time, for example, 1 hour. If you are not very serious, there may still be some such errors, so I tried to implement it myself to see if I could pass it once. The result is self-evident, although the time is not long, I failed.

I personally think that the main cause of failure is that I did not seriously think about this idea and possible branch situations first, but directly wrote code based on subjective assumptions. This completely matches the behavior described in the book, so, as mentioned in the book, an error occurs. After debugging, the basic correct algorithm is obtained. The content is as follows:

#!/usr/bin/env python#encoding: utf-8def half_search(search_arr, search_str):  lb = 0  ub = len(search_arr) - 1  for i in range(ub/2 + 1):    if lb > ub:      return -1    mid = (ub + lb)/2    if search_arr[mid] == search_str:      return mid    elif search_arr[mid] > search_str:      ub = mid - 1    else:      lb = mid + 1if __name__=='__main__':  arr = [10,20,30,40,50,60,70]  print half_search(arr, 1)  print half_search(arr, 11)  print half_search(arr, 22)  print half_search(arr, 33)  print half_search(arr, 40)  print half_search(arr, 55)  print half_search(arr, 66)  print half_search(arr, 70)  print half_search(arr, 8)

Result:

-1 -1 -1 -1 3 -1 -1 6 -1

A positive integer represents the subscript in the array, and 3 represents the first position.-1 indicates that the subscript does not exist.

Summary:

Before implementing a simple algorithm, if you already have a set of simplest implementations, such as directly printing 100 similar content ], consider whether there are more elaborate implementations [Can we replace them with loops + parameterization]. When we implement algorithms with slightly complex points, we may first draw various possible verification conditions on paper, avoid the lack of arm and legs. Another point is that the algorithm should be practiced more. Otherwise, the details may be forgotten after a while. I think this is called specialized technology!

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.