Bubble sort: Return a number to a digit each time by comparing it to neighboring elements
Python code:
"""Bubble Sort (2) in the unsorted number, by 22 comparison [transposition], the smallest number to the first n number of orders, you need to execute the N-1 wheel, the 1th round of N-1 times, the subsequent round than the previous round less than 1 times in this case: the 1th round will be 0 in the correct position; 2nd wheel will 1 in the correct position; ...... The 9th round puts 8 in the correct position;"""LST= [3, 6, 9, 1, 8, 7, 2, 5, 4, 0]Print("before sorting:%s\r\n"%lst)#Wheel forIinchRange (len (LST)-1): Print("section%s round"% (i+1)) #Times forJinchRange (i+1, Len (LST)):#If the front size is small then the swap position ifLst[i] >Lst[j]: lst[i],lst[j]=Lst[j],lst[i]Print("%s times [%s]:%s,[%s]:%s comparison swap%s"% (J-i,i,lst[j],j,lst[i],lst)) Else: Print("first%s times [%s]:%s,[%s]:%s comparison invariant%s"% (J-i,i,lst[i],j,lst[j],lst))Print("\ r \ n Sort after:%s"% LST)
Output Result:
E:\python\algorithm>Python3 bubblesort2.py before sorting: [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] 1th round 1th time [0]:3,[1]:6 comparison invariant [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] 2nd time [0]:3,[2]:9 comparison invariant [3, 6, 9, 1, 8, 7, 2, 5, 4, 0] 3rd time [0]:3,[3]:1 comparison Interchange [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 4th time [0]:1,[4]:8 comparison invariant [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 5th time [0]:1,[5]:7 comparison invariant [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 6th time [0]:1,[6]:2 comparison invariant [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 7th time [0]:1,[7]:5 comparison invariant [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 8th time [0]:1,[8]:4 comparison invariant [1, 6, 9, 3, 8, 7, 2, 5, 4, 0] 9th time [0]:1,[9]:0 comparison Interchange [0, 6, 9, 3, 8, 7, 2, 5, 4, 1] 2nd round 1th time [1]:6,[2]:9 comparison invariant [0, 6, 9, 3, 8, 7, 2, 5, 4, 1] 2nd time [1]:6,[3]:3 comparison Interchange [0, 3, 9, 6, 8, 7, 2, 5, 4, 1] 3rd time [1]:3,[4]:8 comparison invariant [0, 3, 9, 6, 8, 7, 2, 5, 4, 1] 4th time [1]:3,[5]:7 comparison invariant [0, 3, 9, 6, 8, 7, 2, 5, 4, 1] 5th time [1]:3,[6]:2 comparison Interchange [0, 2, 9, 6, 8, 7, 3, 5, 4, 1] 6th time [1]:2,[7]:5 comparison invariant [0, 2, 9, 6, 8, 7, 3, 5, 4, 1] 7th time [1]:2,[8]:4 comparison invariant [0, 2, 9, 6, 8, 7, 3, 5, 4, 1] 8th time [1]:2,[9]:1 comparison Interchange [0, 1, 9, 6, 8, 7, 3, 5, 4, 2] 3rd round 1th time [2]:9,[3]:6 comparison Interchange [0, 1, 6, 9, 8, 7, 3, 5, 4, 2] 2nd time [2]:6,[4]:8 comparison invariant [0, 1, 6, 9, 8, 7, 3, 5, 4, 2] 3rd time [2]:6,[5]:7 comparison invariant [0, 1, 6, 9, 8, 7, 3, 5, 4, 2] 4th time [2]:6,[6]:3 comparison Interchange [0, 1, 3, 9, 8, 7, 6, 5, 4, 2] 5th time [2]:3,[7]:5 comparison invariant [0, 1, 3, 9, 8, 7, 6, 5, 4, 2] 6th time [2]:3,[8]:4 comparison invariant [0, 1, 3, 9, 8, 7, 6, 5, 4, 2] 7th time [2]:3,[9]:2 comparison Interchange [0, 1, 2, 9, 8, 7, 6, 5, 4, 3] 4th round 1th Time [3]:9,[4]:8 comparison Interchange [0, 1, 2, 8, 9, 7, 6, 5, 4, 3] 2nd time [3]:8,[5]:7 comparison Interchange [0, 1, 2, 7, 9, 8, 6, 5, 4, 3] 3rd time [3]:7,[6]:6 comparison Interchange [0, 1, 2, 6, 9, 8, 7, 5, 4, 3] 4th time [3]:6,[7]:5 comparison Interchange [0, 1, 2, 5, 9, 8, 7, 6, 4, 3] 5th time [3]:5,[8]:4 comparison Interchange [0, 1, 2, 4, 9, 8, 7, 6, 5, 3] 6th time [3]:4,[9]:3 comparison Interchange [0, 1, 2, 3, 9, 8, 7, 6, 5, 4] 5th round 1th time [4]:9,[5]:8 comparison Interchange [0, 1, 2, 3, 8, 9, 7, 6, 5, 4] 2nd time [4]:8,[6]:7 comparison Interchange [0, 1, 2, 3, 7, 9, 8, 6, 5, 4] 3rd time [4]:7,[7]:6 comparison Interchange [0, 1, 2, 3, 6, 9, 8, 7, 5, 4] 4th time [4]:6,[8]:5 comparison Interchange [0, 1, 2, 3, 5, 9, 8, 7, 6, 4] 5th time [4]:5,[9]:4 comparison Interchange [0, 1, 2, 3, 4, 9, 8, 7, 6, 5] 6th round 1th time [5]:9,[6]:8 comparison Interchange [0, 1, 2, 3, 4, 8, 9, 7, 6, 5] 2nd time [5]:8,[7]:7 comparison Interchange [0, 1, 2, 3, 4, 7, 9, 8, 6, 5] 3rd time [5]:7,[8]:6 comparison Interchange [0, 1, 2, 3, 4, 6, 9, 8, 7, 5] 4th time [5]:6,[9]:5 comparison Interchange [0, 1, 2, 3, 4, 5, 9, 8, 7, 6] 7th round 1th Time [6]:9,[7]:8 comparison Interchange [0, 1, 2, 3, 4, 5, 8, 9, 7, 6] 2nd time [6]:8,[8]:7 comparison Interchange [0, 1, 2, 3, 4, 5, 7, 9, 8, 6] 3rd time [6]:7,[9]:6 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 9, 8, 7] 8th round 1th time [7]:9,[8]:8 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 8, 9, 7] 2nd time [7]:8,[9]:7 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 7, 9, 8] 9th round 1th time [8]:9,[9]:8 comparison Interchange [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 (2)