Bubble sort note three points:
1. The first cycle does not have to cycle through all elements.
2. The two-layer cyclic variable is associated with the first-level loop variable.
3. Second-tier loops, which eventually must loop through all the elements in the collection.
Sample code One:
1. The first cycle, only n-1 elements.
2. When the first-level loop variable is n-1, the second layer loops through all the elements.
s = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]
# Bubble_sort for
i in range (0, Len (s)-1): For
J in range (i + 1, 0,-1): C7/>if S[j] < S[j-1]:
s[j], s[j-1] = s[j-1], s[j] for
m in range (0, Len (s)):
print (s[m))
Example code two:
1. The first layer loops all elements.
2. The second layer also circulates all elements.
s = [3 4, 1, 6, 2, 9, 7, 0, 8, 5] for
I in range (0, Len (s)):
for J in range (I, 0,-1):
if S[J] < s[j- 1]:
s[j], s[j-1] = s[j-1], s[j] for
m in range (0, Len (s)):
print (s[m))
The above is a small series to introduce the Python bubble sorting algorithm attention points, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!