Bubble sort
The basic idea of bubble sorting is to compare two adjacent elements each time and swap them out if they are in the wrong order.
If there are n numbers to sort, just n?1 the number of digits, which means
N-1 operation. The "Every trip" requires a comparison of two adjacent numbers starting from the 1th position, and the smaller one
In the back, when the comparison is complete, move back one to continue comparing the size of the two adjacent numbers below, repeat this step until the last
Number that has not been returned, the number that has been returned does not need to be compared.
Code
1 def_bubblesort ():2A = []3n = Int (input ('Do you need to enter a few numbers to sort? '))4K=15 forIinchrange (n):6A.append (int (Input ('Please enter the number of%i:'%k )))7K+=18 9 forIinchRange (n-1):Ten forJinchRange (n-i-1): One ifA[J] < a[j+1]: AA[J], a[j+1] = a[j+1], A[j] - Print 'The bubble sort result is:' - forIinchA: the PrintI - - if __name__=='__main__': -_bubblesort ()
Python 2.7.9
Python implements the bubbling ordering of data structures and algorithms