Little Luo's Algorithm--Bisection Algorithm


What can two points do?

二分是一种常用的算法技巧,用于在有序数组或有序序列中查找某个元素的位置。

The basic idea of ​​the binary algorithm is to continuously narrow the search interval until the target element is found or it is determined that the target element does not exist. The specific implementation method is to first determine the left and right boundaries of the search interval, and then calculate the element value in the middle position. If the value is equal to the target value, the target element is found; if the value is greater than the target value, it means that the target element should be searched in the left half. Otherwise, search in the right half. Keep narrowing down the search interval until the target element is found or it is determined that the target element does not exist.

于是我们引入红蓝二分的思想来更好的引入二分

1. Learning dichotomy

The time complexity of the binary algorithm is O(log n), where n is the size of the array or sequence, which has better time efficiency than the time complexity O(n) of violent enumeration

Use conditions and difficulties

Although the binary algorithm looks simple, it actually has some usage conditions and difficulties:

  1. The most painful thing is the boundary condition :
    when implementing the binary algorithm, the boundary condition is very critical. Boundary conditions include the start and end points of the search interval, and how the intermediate elements are calculated. If the boundary conditions are incorrect, it may lead to errors in the algorithm or enter an infinite loop.

  2. The array must be ordered :
    Since the core idea of ​​the binary algorithm is to gradually narrow the search interval, it is necessary to ensure that the array is ordered. If the array is unordered, it needs to be sorted first, which will increase the complexity of the algorithm.

  3. The most important thing is to be able to see that it can use bifurcation (state transition design):
    such as: dynamic programming + dichotomous leetcode.300 , ordinary people really don’t know how to use dichotomy, and I was thinking about what their brains are doing of!

topic introduction

Red and blue dichotomy
Let's start with a simple dichotomy
Now I want to find the number in an array:

list_num = [ i for i in range(0,100,2)]

# 找22在这个数组的哪里,或者22有没有在这个数组里面
left = -1
right = len(list_num)
while left+1!=right:
	mid = (left+right)//2
	if list_num[mid]<=22:
		left = mid
	else:
		right = mid
return left

This is written by a classic red-blue dichotomy. See his condition left+1!=right, so we will not have the intersection of left and right.
But our answer has already been put into left and right

Let's explain in detail what is the red and blue dichotomy
. left, right is the boundary of red and blue dichotomy. As the name suggests, one is the right boundary and the other is the left boundary. The borders will not intersect, but will only be adjacent if the condition left+1==right is met. That is, when the While loop exits

Then there is the initial left, what is the initial value for right:
left:-1
right:len(list)
In this way, we don’t have to think about the boundary problem, because either the area of ​​the list is completely occupied by left, or it is occupied by right .
And we cleverly bypassed the boundary problem of the two points by using the method of red and blue
points Design, being able to write dichotomy is not what matters is that you can see that this can use dichotomy. Many algorithms are not written by you, but you can see that it can be used!
insert image description here

2. Python's built-in binary function

This library is really easy to use, and it is built-in and works well in algorithmic competitions

Binary functions in Python can be implemented using the standard library bisect. The bisect module provides two functions: bisect_left() and bisect_right(). These functions use the binary search algorithm to find the position of a specified element in an ordered sequence.

The difference between these two functions is that when there are multiple identical elements, the bisect_left() function returns the position of the first occurrence, while bisect_right() returns the position of the last occurrence.

import bisect

lst = [1, 3, 4, 4, 4, 6, 8, 9]
x = 4

Use bisect_left() to find the position of x

idx_left = bisect.bisect_left(lst, x)
print("bisect_left(): ", idx_left)

Use bisect_right() to find the position of x

idx_right = bisect.bisect_right(lst, x)
print("bisect_right(): ", idx_right)
输出:
bisect_left():  2
bisect_right():  5
在上面的示例中,列表lst中有多个值为4的元素。
bisect_left()函数返回第一个出现的位置2,
而bisect_right()函数返回最后一个出现的位置5

Guess you like

Origin blog.csdn.net/L2489754250/article/details/129491645