Python code:
"""Bubble Sort (5) A variant of two-way bubbling, bubbling sort. In unsorted numbers, each round finds the minimum and maximum number of digits to be placed in the end. In this example: 1th round: First put 9 in the correct position, then put 0 in the correct position, the 2nd round: first put 8 in the correct position, and then put 1 in the correct position, 3rd round: first put 7 in the correct position, and then put 2 in the correct position; 4th wheel: First put 6 in the correct position, and then put 3 in the correct position 5th round: First put 5 in the correct position, and then put 4 in the correct position;"""LST= [3, 6, 9, 1, 8, 7, 2, 5, 4, 0]Print("before sorting:%s\r\n"%lst)#Wheel forIinchRange (int (len (LST)/2)): Print("section%s round"% (i+1)) #Times forMinchRange (I,len (LST)-i-1): #If the front size is small then the swap position ifLST[M] > Lst[m+1]: lst[m],lst[m+1] = lst[m+1],lst[m]Print("right Bubble first%s [%s]:%s,[%s]:%s compare swap%s"% (m-i+1,m,lst[m+1],m+1, Lst[m],lst)) Else: Print("right-to-bubble-%s-times [%s]:%s,[%s]:%s comparison invariant%s"% (m-i+1,m,lst[m],m+1,lst[m+1],lst))Print("\ r \ n") #Times forNinchRange (len (LST)-i-2,i,-1): #If the front size is small then the swap position ifLst[n-1] >Lst[n]: lst[n-1],lst[n] = lst[n],lst[n-1] Print("left Bubble First%s [%s]:%s,[%s]:%s compare swap%s"% (9-n-i,n-1,lst[n],n,lst[n-1],lst))Else: Print("left bubble first%s times [%s]:%s,[%s]:%s comparison invariant%s"% (9-n-i,n-1,lst[n-1],n,lst[n],lst))Print("\ r \ n Sort after:%s"% LST)
Output Result:
E:\python\algorithm>Python3 bubblesort5.py before sorting: [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] 1th round right bubbles 1th time [0]:3,[1]:6 comparison invariant [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] right-bubbling for the 2nd time [1]:6,[2]:9 comparison invariant [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] right-bubbling for the 3rd time [2]:9,[3]:1 comparison Interchange [3, 6, 1, 9, 8, 7, 2, 5, 4, 0] right-bubbling for the 4th time [3]:9,[4]:8 comparison Interchange [3, 6, 1, 8, 9, 7, 2, 5, 4, 0] right-bubbling for the 5th time [4]:9,[5]:7 comparison Interchange [3, 6, 1, 8, 7, 9, 2, 5, 4, 0] right-bubbling for the 6th time [5]:9,[6]:2 comparison Interchange [3, 6, 1, 8, 7, 2, 9, 5, 4, 0] right-bubbling for the 7th time [6]:9,[7]:5 comparison Interchange [3, 6, 1, 8, 7, 2, 5, 9, 4, 0] right-bubbling for the 8th time [7]:9,[8]:4 comparison Interchange [3, 6, 1, 8, 7, 2, 5, 4, 9, 0] right-bubbling for the 9th time [8]:9,[9]:0 comparison Interchange [3, 6, 1, 8, 7, 2, 5, 4, 0, 9] Left-bubbling for the 1th time [7]:4,[8]:0 comparison Interchange [3, 6, 1, 8, 7, 2, 5, 0, 4, 9] Left-bubbling for the 2nd time [6]:5,[7]:0 comparison Interchange [3, 6, 1, 8, 7, 2, 0, 5, 4, 9] Left-bubbling for the 3rd time [5]:2,[6]:0 comparison Interchange [3, 6, 1, 8, 7, 0, 2, 5, 4, 9] Left-bubbling for the 4th time [4]:7,[5]:0 comparison Interchange [3, 6, 1, 8, 0, 7, 2, 5, 4, 9] Left-bubbling for the 5th time [3]:8,[4]:0 comparison Interchange [3, 6, 1, 0, 8, 7, 2, 5, 4, 9] Left-bubbling for the 6th time [2]:1,[3]:0 comparison Interchange [3, 6, 0, 1, 8, 7, 2, 5, 4, 9] Left-bubbling for the 7th time [1]:6,[2]:0 comparison interchange [3, 0, 6, 1, 8, 7, 2, 5, 4, 9] Left-bubbling for the 8th time [0]:3,[1]:0 comparison Interchange [0, 3, 6, 1, 8, 7, 2, 5, 4, 9] 2nd round right to bubble 1th time [1]:3,[2]:6 comparison invariant [0, 3, 6, 1, 8, 7, 2, 5, 4, 9] Right-bubbling for the 2nd time [2]:6,[3]:1 comparison Interchange [0, 3, 1, 6, 8, 7, 2, 5, 4, 9] Right-bubbling for the 3rd time [3]:6,[4]:8 comparison invariant [0, 3, 1, 6, 8, 7, 2, 5, 4, 9] Right-bubbling for the 4th time [4]:8,[5]:7 comparison Interchange [0, 3, 1, 6, 7, 8, 2, 5, 4, 9] Right-bubbling for the 5th time [5]:8,[6]:2 comparison Interchange [0, 3, 1, 6, 7, 2, 8, 5, 4, 9] Right-bubbling for the 6th time [6]:8,[7]:5 comparison Interchange [0, 3, 1, 6, 7, 2, 5, 8, 4, 9] Right-bubbling for the 7th time [7]:8,[8]:4 comparison Interchange [0, 3, 1, 6, 7, 2, 5, 4, 8, 9] Left-bubbling for the 1th time [6]:5,[7]:4 comparison Interchange [0, 3, 1, 6, 7, 2, 4, 5, 8, 9] Left-bubbling for the 2nd time [5]:2,[6]:4 comparison invariant [0, 3, 1, 6, 7, 2, 4, 5, 8, 9] Left-bubbling for the 3rd time [4]:7,[5]:2 comparison Interchange [0, 3, 1, 6, 2, 7, 4, 5, 8, 9] Left-bubbling for the 4th time [3]:6,[4]:2 comparison Interchange [0, 3, 1, 2, 6, 7, 4, 5, 8, 9] Left-bubbling for the 5th time [2]:1,[3]:2 comparison invariant [0, 3, 1, 2, 6, 7, 4, 5, 8, 9] Left-bubbling for the 6th time [1]:3,[2]:1 comparison Interchange [0, 1, 3, 2, 6, 7, 4, 5, 8, 9] 3rd round right to bubble 1th time [2]:3,[3]:2 comparison Interchange [0, 1, 2, 3, 6, 7, 4, 5, 8, 9] Right-bubbling for the 2nd time [3]:3,[4]:6 comparison invariant [0, 1, 2, 3, 6, 7, 4, 5, 8, 9] Right-bubbling for the 3rd time [4]:6,[5]:7 comparison invariant [0, 1, 2, 3, 6, 7, 4, 5, 8, 9] Right-bubbling for the 4th time [5]:7,[6]:4 comparison Interchange [0, 1, 2, 3, 6, 4, 7, 5, 8, 9] Right-bubbling for the 5th time [6]:7,[7]:5 comparison Interchange [0, 1, 2, 3, 6, 4, 5, 7, 8, 9] Left-bubbling for the 1th time [5]:4,[6]:5 comparison invariant [0, 1, 2, 3, 6, 4, 5, 7, 8, 9] Left-bubbling for the 2nd time [4]:6,[5]:4 comparison Interchange [0, 1, 2, 3, 4, 6, 5, 7, 8, 9] Left-bubbling for the 3rd time [3]:3,[4]:4 comparison invariant [0, 1, 2, 3, 4, 6, 5, 7, 8, 9] Left-bubbling for the 4th time [2]:2,[3]:3 comparison invariant [0, 1, 2, 3, 4, 6, 5, 7, 8, 9] 4th round right to bubble 1th time [3]:3,[4]:4 comparison invariant [0, 1, 2, 3, 4, 6, 5, 7, 8, 9] Right-bubbling for the 2nd time [4]:4,[5]:6 comparison invariant [0, 1, 2, 3, 4, 6, 5, 7, 8, 9] Right-bubbling for the 3rd time [5]:6,[6]:5 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Left-bubbling for the 1th time [4]:4,[5]:5 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Left-bubbling for the 2nd time [3]:3,[4]:4 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 5th round right to bubble 1th time [4]:4,[5]:5 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After sorting: [0,1, 2, 3, 4, 5, 6, 7, 8, 9]
===== End =====
Python bubble sort (5)