Python algorithm (i) bubble sort

Source: Internet
Author: User

3 5 1) 6 2

1) First: Find the largest of these numbers and put it at the end.

3, 5 Find a large number in the second position

5, 1 Find a large number in the third position

5, 6 Find a large number in the fourth position

2, 6 find a large number to enlarge the fifth position

The fifth position is the largest one.

a= [3,5,1,6,2]
For I in range (Len (a)-1):
If A[I]>A[I+1]:
A[I],A[I+1] = A[i+1],a[i]

Print (A[-1]) 2) found the maximum value, now find the second big value, the second largest value in the penultimate position a= [3, 1, 5, 2, 6]
For I in range (Len (a)-1-1):
If A[I]>A[I+1]:
A[I],A[I+1] = A[i+1],a[i]

Print (A[-2]) 3) Find the third largest number, and put it to the last third for I in Range (Len (a) -1-1-1):
If A[I]>A[I+1]:
A[I],A[I+1] = A[i+1],a[i]

Print (a[-3]) 4) Find the fourth largest number and put it in the bottom fourth for I in Range (Len (a) -1-1-1-1):
If A[I]>A[I+1]:
A[I],A[I+1] = A[i+1],a[i]

Print (A[-4]) 5) The last one left is the smallest number, put to the first to find the law, to realize the bubbling a= [3,5,1,6,2]
For I in range (Len (a)-1): #0,
For j in Range (Len (a) -1-i):
If A[J]>A[J+1]:
A[J],A[J+1] = A[j+1],a[j]

Print (a) summarize the process: The result of the first inner loop is to find the maximum value
The result of the second inner loop is to find the second largest value, this time ignoring the comparison of the last element
The result of the third inner loop is to find the third largest value, this time ignoring the second and last element of the comparison the fourth inner loop results in finding the fourth largest value, this time ignoring the third and second-to-last element and the final element of the comparison of the last, is the smallest number
.......

Two numbers in Python how to interact in a location

Two types of notation:

1.

A, B = b,a

2.

Temp=a

A=b

B=temp

a=[7,2,4,21,44,3]

two x for cycle, the first layer control than several times, the second layer of control how than

Ascending

For I in range (1,len (a)):

For j in Range (Len (a)-1):

If A[j]>a[i]:

A[J],A[I]=A[I],A[J]

Print a

Descending

For I in range (1,len (a)):

For j in Range (Len (a)-1):

If A[j]<a[i]:

A[J],A[I]=A[I],A[J]

Print a

two x for cycle, the first layer control than several times, the second layer of control how than

Ascending

For I in range (1,len (a)):

for J in Range (Len (a)-1):

if a[j]>a[i]:

A[j],a[i]=a[i],a[j]

Print a

Descending

For I in range (1,len (a)):

for J in Range (Len (a)-1):

if a[j]<a[i]:

A[j],a[i]=a[i],a[j]

Print a

Python algorithm (i) bubble sort

Related Article

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.