A sort of permutation :
From the first pair to the last pair, each pair compares the size , If the former is greater than the latter, the exchange is , therefore The last element It's the biggest ;
Then consider The second largest number , Put it in the penultimate position according to the principle ;
Reduce the length of each cycle by one , Because every time I put the big numbers behind , When it's ready, consider a smaller number , Put these numbers in place again .
def bubble_sort(arr): length = len(arr) while length > 0: for i in range(length - 1): if arr[i] > arr[i + 1]: