樹心幽徑

« 20191109製作Ubuntu1604Linux安裝隨身碟並將之安裝到另一顆隨身碟中。 | Main | 20191111用python編寫選擇排序法程式(採升序排列,ascending) »

20191111用python設計氣泡排序法(採升序排列,ascending)
2019/11/11,09:25

(一)編寫bsort.py 程式碼如下:   bsort.py下載 (484 bytes) | created 11 十一月, 2019

cs = [-1,5,8,1,3]
print ("1:Before bubble sort: cs=",cs)


print ("2: Sorting.........")
for i in range(1,len(cs)-1):
    print ("Step i=", i)
    for j in range(1,len(cs)-i):
            if cs[j] > cs[j+1] :
                print ("    swap at j=",j, "(",cs[j] ,",",cs[j+1],")")
                t=cs[j]
                cs[j]=cs[j+1]
                cs[j+1]=t
            else:
                print ("    no swap at j=",j, "(",cs[j] ,",",cs[j+1],")")

            print ("    =>cs=",cs, "j=",j)

    print
  
print ("3:After bubble sort: cs=",cs)

(二)執行結果如下:

1:Before bubble sort: cs= [-1, 5, 8, 1, 3]
2: Sorting.........
Step i= 1
    no swap at j= 1 ( 5 , 8 )
    =>cs= [-1, 5, 8, 1, 3] j= 1
    swap at j= 2 ( 8 , 1 )
    =>cs= [-1, 5, 1, 8, 3] j= 2
    swap at j= 3 ( 8 , 3 )
    =>cs= [-1, 5, 1, 3, 8] j= 3
Step i= 2
    swap at j= 1 ( 5 , 1 )
    =>cs= [-1, 1, 5, 3, 8] j= 1
    swap at j= 2 ( 5 , 3 )
    =>cs= [-1, 1, 3, 5, 8] j= 2
Step i= 3
    no swap at j= 1 ( 1 , 3 )
    =>cs= [-1, 1, 3, 5, 8] j= 1
3:After bubble sort: cs= [-1, 1, 3, 5, 8]

 

(三)執行結果2如下(修改第一行程式碼為 cs= [-1, 5, 8, 1, 3, 44, 33, 2, 21])

1:Before bubble sort: cs= [-1, 5, 8, 1, 3, 44, 33, 2, 21]
2: Sorting.........
Step i= 1
    no swap at j= 1 ( 5 , 8 )
    =>cs= [-1, 5, 8, 1, 3, 44, 33, 2, 21] j= 1
    swap at j= 2 ( 8 , 1 )
    =>cs= [-1, 5, 1, 8, 3, 44, 33, 2, 21] j= 2
    swap at j= 3 ( 8 , 3 )
    =>cs= [-1, 5, 1, 3, 8, 44, 33, 2, 21] j= 3
    no swap at j= 4 ( 8 , 44 )
    =>cs= [-1, 5, 1, 3, 8, 44, 33, 2, 21] j= 4
    swap at j= 5 ( 44 , 33 )
    =>cs= [-1, 5, 1, 3, 8, 33, 44, 2, 21] j= 5
    swap at j= 6 ( 44 , 2 )
    =>cs= [-1, 5, 1, 3, 8, 33, 2, 44, 21] j= 6
    swap at j= 7 ( 44 , 21 )
    =>cs= [-1, 5, 1, 3, 8, 33, 2, 21, 44] j= 7
Step i= 2
    swap at j= 1 ( 5 , 1 )
    =>cs= [-1, 1, 5, 3, 8, 33, 2, 21, 44] j= 1
    swap at j= 2 ( 5 , 3 )
    =>cs= [-1, 1, 3, 5, 8, 33, 2, 21, 44] j= 2
    no swap at j= 3 ( 5 , 8 )
    =>cs= [-1, 1, 3, 5, 8, 33, 2, 21, 44] j= 3
    no swap at j= 4 ( 8 , 33 )
    =>cs= [-1, 1, 3, 5, 8, 33, 2, 21, 44] j= 4
    swap at j= 5 ( 33 , 2 )
    =>cs= [-1, 1, 3, 5, 8, 2, 33, 21, 44] j= 5
    swap at j= 6 ( 33 , 21 )
    =>cs= [-1, 1, 3, 5, 8, 2, 21, 33, 44] j= 6
Step i= 3
    no swap at j= 1 ( 1 , 3 )
    =>cs= [-1, 1, 3, 5, 8, 2, 21, 33, 44] j= 1
    no swap at j= 2 ( 3 , 5 )
    =>cs= [-1, 1, 3, 5, 8, 2, 21, 33, 44] j= 2
    no swap at j= 3 ( 5 , 8 )
    =>cs= [-1, 1, 3, 5, 8, 2, 21, 33, 44] j= 3
    swap at j= 4 ( 8 , 2 )
    =>cs= [-1, 1, 3, 5, 2, 8, 21, 33, 44] j= 4
    no swap at j= 5 ( 8 , 21 )
    =>cs= [-1, 1, 3, 5, 2, 8, 21, 33, 44] j= 5
Step i= 4
    no swap at j= 1 ( 1 , 3 )
    =>cs= [-1, 1, 3, 5, 2, 8, 21, 33, 44] j= 1
    no swap at j= 2 ( 3 , 5 )
    =>cs= [-1, 1, 3, 5, 2, 8, 21, 33, 44] j= 2
    swap at j= 3 ( 5 , 2 )
    =>cs= [-1, 1, 3, 2, 5, 8, 21, 33, 44] j= 3
    no swap at j= 4 ( 5 , 8 )
    =>cs= [-1, 1, 3, 2, 5, 8, 21, 33, 44] j= 4
Step i= 5
    no swap at j= 1 ( 1 , 3 )
    =>cs= [-1, 1, 3, 2, 5, 8, 21, 33, 44] j= 1
    swap at j= 2 ( 3 , 2 )
    =>cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44] j= 2
    no swap at j= 3 ( 3 , 5 )
    =>cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44] j= 3
Step i= 6
    no swap at j= 1 ( 1 , 2 )
    =>cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44] j= 1
    no swap at j= 2 ( 2 , 3 )
    =>cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44] j= 2
Step i= 7
    no swap at j= 1 ( 1 , 2 )
    =>cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44] j= 1
3:After bubble sort: cs= [-1, 1, 2, 3, 5, 8, 21, 33, 44]


迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType