Python bubble sort (6)

Source: Internet
Author: User

There are 5 kinds of bubbles in the front, only 2 bubbles in essence.

The 1th is to sort by a two-digit comparison, and the 2nd is to be sorted in the same way as fixed-digit comparisons.

The position of the two comparisons in the 1th way is different each time, and the 2nd way, where two of the comparison numbers are located, is unchanged.

In the 1th way, you can reduce the sort round by adding a way to judge the identity.

For example, the following code adds the BF ID

Python code:

LST = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]Print("before sorting:%s\r\n"%lst)#Wheel forIinchRange (int (len (LST)/2)): BF=FalsePrint("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] BF=TruePrint("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")    if  notBF: Break    #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] BF=TruePrint("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))if  notBF: BreakPrint("\ r \ n Sort after:%s"% LST)

Output Result:

E:\python\algorithm>Python3 bubblesort5.py before sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] 1th round right bubbles 1th time [0]:1,[1]:2 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 2nd time [1]:2,[2]:3 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 3rd time [2]:3,[3]:4 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 4th time [3]:4,[4]:5 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 5th time [4]:5,[5]:6 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 6th time [5]:6,[6]:7 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 7th time [6]:7,[7]:8 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 8th time [7]:8,[8]:9 comparison invariant [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] right-bubbling for the 9th time [8]:9,[9]:0 comparison Interchange [1, 2, 3, 4, 5, 6, 7, 8, 0, 9] Left-bubbling for the 1th time [7]:8,[8]:0 comparison Interchange [1, 2, 3, 4, 5, 6, 7, 0, 8, 9] Left-bubbling for the 2nd time [6]:7,[7]:0 comparison Interchange [1, 2, 3, 4, 5, 6, 0, 7, 8, 9] Left-bubbling for the 3rd time [5]:6,[6]:0 comparison Interchange [1, 2, 3, 4, 5, 0, 6, 7, 8, 9] Left-bubbling for the 4th time [4]:5,[5]:0 comparison Interchange [1, 2, 3, 4, 0, 5, 6, 7, 8, 9] Left-bubbling for the 5th time [3]:4,[4]:0 comparison Interchange [1, 2, 3, 0, 4, 5, 6, 7, 8, 9] Left-bubbling for the 6th time [2]:3,[3]:0 comparison Interchange [1, 2, 0, 3, 4, 5, 6, 7, 8, 9] Left-bubbling for the 7th time [1]:2,[2]:0 comparison interchange [1, 0, 2, 3, 4, 5, 6, 7, 8, 9] Left-bubbling for the 8th time [0]:1,[1]:0 comparison Interchange [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2nd round right to bubble 1th time [1]:1,[2]:2 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 2nd time [2]:2,[3]:3 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 3rd time [3]:3,[4]:4 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 4th time [4]:4,[5]:5 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 5th time [5]:5,[6]:6 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 6th time [6]:6,[7]:7 comparison invariant [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Right-bubbling for the 7th time [7]:7,[8]:8 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 (6)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.