data = [Ten, 4, 3, 8, one, 5, ten, 2, 1, +, +] "' idea: How many elements are looped, and each cycle starts from the first element and compares it to the elements behind it, if Larger than the back, in exchange, the largest number of the first cycle will be placed at the end, the next cycle will be less traversing a single number. ' for in Range ' (Len (data)-1): the For in range (len (data ) (j): if data[i] > data[i+1]: data[i], data[i+1] = data[i+1], Data[i]print(data)
Modify the code so that it prints out the results after each loop
data = [Ten, 4, 3, 8, one, 5, six, 2, 1, +, +] "' idea: How many elements are looped, starting from the first element and comparing it to the elements behind it, if the "In range "(len (data)-1): for in range (len (data)j): if data[i] > data[i+1]: data[i], data[i+1] = data[i+1 ], Data[i] print(data)
Print
C:\temp>Python3 test.py[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 54][4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 33, 54][4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 22, 33, 54][4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 21, 22, 33, 54][3, 4, 8, 5, 10, 2, 1, 11, 13, 17, 21, 22, 33, 54][3, 4, 5, 8, 2, 1, 10, 11, 13, 17, 21, 22, 33, 54][3, 4, 5, 2, 1, 8, 10, 11, 13, 17, 21, 22, 33, 54][3, 4, 2, 1, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][3, 2, 1, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][2, 1, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54]
Python (5)-bubble sort