Python Bubble Sorting example details, python bubble
Three notes for Bubble Sorting:
1. You do not need to cycle all elements in the first loop.
2. Two-layer cyclic variables are associated with the first-layer cyclic variables.
3. The second layer of the loop must eventually loop all elements in the set.
Sample Code 1:
1. Loop at the first layer, which only loops n-1 elements.
2. When the first loop variable is n-1, the second loop loops all elements.
S = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5] # bubble_sortfor I in range (0, len (s)-1 ): for j in range (I + 1, 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])
Sample Code 2:
1. The first layer loops all elements.
2. The second layer also loops through 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 the key points of attention for the python Bubble sorting algorithm introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!