Sort list element size in Python

Source: Internet
Author: User

  #对lt列表里元素从小到大排序 

#一, select Sort by

LT = [3, 5, 2, 1, 8, 4]
#求出lt的长度
n = Len (LT)
#外层循环确定比较的轮数, X is subscript, lt[x] represents all elements of LT in the outer loop
for x in Range (n-1):
#内层循环开始比较
for y in Range (x+1,n):
#lt [x] represents a specific element in the for Y Loop, and Lt [y] represents any one of the LT elements.
if Lt[x]>lt[y]:
#让lt [x] Compare each element in the LT list to find a small
Lt[x],lt[y]=lt[y],lt[x]
print (LT)

#二, sort by bubble sort
lt = [3, 5, 2, 1, 8, 4]
n= Len (LT)
For x in range (n-1):
For y in range (n-1-x):
If LT[Y]>LT[Y+1]:
Lt[y],lt[y+1]=lt[y+1],lt[y]
Print (LT)

Sort list element size in 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.