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