This article introduces the python bubble sorting knowledge, which involves the main details of the bubble sorting. This article uses the example code to explain it to you, which is very detailed and has reference value, if you are interested, let's take a look. This article introduces you to the python bubble sorting knowledge, which involves the main details of bubble sorting. This article uses the example code to explain to you, which is very detailed, if you are interested in the reference, take a look.
">
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_sort
For 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.
The above is a detailed description of the main points of attention for Python bubble sorting. For more information, see other related articles in the first PHP community!