Get the App
SLTechnology News&Howtos  ›  Development  › 

Python dichotomy to find out how to use it

Shulou Source: shulou.com Published: 2022-06-02 12:38:57 09月11日 Update

This article mainly explains "how to use python dichotomy search". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to use python dichotomy search" together!

For more elements to search, binary search speed is faster than simple search This is the advantage of binary search algorithm, but binary algorithm also has disadvantages, binary algorithm only for ordered lists, so insertion and deletion will be very difficult, therefore, binary search method is only suitable for ordered lists that do not change often

Binary search has a very important feature, that is, it does not find all the elements of the sequence, and the amount of data found actually coincides with the logarithm of the elements. Under normal circumstances, the elements found each time are reduced by half. The time complexity of a binary search is O(log2n). At best, of course, it's a one-time search, but at worst and in general it's a lot better than a sequential search.

Given an ordered (ascending) array of n integers numbers and a target value target, write a function to search for target in numbers, returning subscripts if the target value exists, otherwise returning-1.

class Solution: def search(self,nums:List[int],target:int)->int: left=0 right=len(nums)-1 while(left

Tags: Element dichotomy case array order subscript algorithm topic learning content function that is more target value target value answer search complexity same Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi MariaDB Shulou Technology Docker