1:2-point method
First of all, introduce the dichotomy method
Binary search, can eliminate half of the data each time, the search efficiency is very high, but the limitations are relatively large, must be ordered sequence can be used to find the binary method
Requirement: The lookup sequence must be an ordered sequence
--------------------------------------------------------------------------------------------------------------- ------------------------------------------
Here is an example of a dichotomy:
# lst=[1,3,6,8,15,22,33,44,45,56,77,78,87,89,90,94,97,101]
lst=[1,2,3,4,5,6]
N=4
Left=0
Right=len (LST)-1
Count=1
While left <= right:
Middle= (left+right)//2
If n < Lst[middle]:
Right=middle-1 #重新定义边界, because the previous middle has been compared, moving to the left one
elif n > Lst[middle]:
Left=middle+1
else: #思想是退出循环就表示找到了
Print (count) #count用来计算查找了几次
Print (middle) #middle是查找到的索引, how does this fix the position?
Break
Count=count+1 #用来计算查找了几次
Else
Print ("Not Present")
--------------------------------------------------------------------------------------------------
Two: Bubbling method:
is to compare the size of the two digits before and after, if the front is larger than the back, swap
Here is an example:
LST=[10,20,2,33,44,66,77,88,45,4,56,77,8,6,643,334,53,2,23]
def fun_sort (LST):
Count=len (LST)
For I in Range (0,count):
For j in Range (I+1,count):
If lst[i] > Lst[j]:
Lst[i],lst[j]=lst[j],lst[i]
Print (LST)
A=fun_sort (LST)
Print (a)
----------------------------------------------------------------------------------------------------
A summary of the bubbling and dichotomy methods of Python