A summary of the bubbling and dichotomy methods of Python

Source: Internet
Author: User

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

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.