python編程冒泡排序法實(shí)現(xiàn)動(dòng)圖排序示例解析
先上個(gè)冒泡排序的效果圖:
是不是,有那么一點(diǎn)點(diǎn)像了? 其實(shí)要做這個(gè)動(dòng)圖真不是很難,來(lái)看冒泡的代碼:
>>> def Bubble(List): L = len(List)-1 for i in range(L): for j in range(L-i): if List[j]>List[j+1]: List[j],List[j+1]=List[j+1],List[j] return List >>> lst = [randint(1,20) for _ in range(15)] >>> lst [1, 10, 4, 18, 3, 15, 8, 8, 20, 12, 14, 14, 20, 6, 19] >>> Bubble(lst) [1, 3, 4, 6, 8, 8, 10, 12, 14, 14, 15, 18, 19, 20, 20]
動(dòng)態(tài)排序的原理
冒泡排序就是在循環(huán)中當(dāng)List[j]>List[j+1]
時(shí)不停交換元素,雙循環(huán)結(jié)果排序即成。那么,在做動(dòng)圖時(shí),除了交換元素,還要交換色塊位置以及數(shù)字標(biāo)注的值。用python自帶的tkinter庫(kù),寫(xiě)gui界面比較容易。添加一個(gè)畫(huà)布canvas和兩個(gè)按鈕button然后用create_rectangle 和 create_text 畫(huà)色塊和文字標(biāo)簽;在冒泡排序的循環(huán)中添加交換它們位置的代碼即可。另外,改變文本和填充顏色用itemconfig()函數(shù),參數(shù)分別用 text和fill。
更多注釋見(jiàn)第三部分;
python常用顏色表參見(jiàn):Python編程tkinter庫(kù)Canvas實(shí)現(xiàn)涂鴉顏色表及圍棋盤(pán)示例
Python tkinter庫(kù)Canvas操作
動(dòng)態(tài)排序的完整代碼
import tkinter as tk from random import randint from time import sleep def init(): global rect,font,pos,lst,step count = 20 rect,font,pos = [0]*count,[0]*count,[] lst = [randint(1,20) for _ in range(count)] x,y = 45,330 width,step = 15,28 cv.delete('all') for i in range(count): pos.append((x+i*step,y-lst[i]*width,x+i*step+width,y)) sleep(0.1) rect[i] = cv.create_rectangle(pos[i], fill='royalblue') font[i] = cv.create_text(x+i*step+7,y+10,text=str(lst[i]),anchor=tk.CENTER) cv.update() btn2.configure(state=tk.NORMAL) btn1.configure(state=tk.DISABLED) def bubble(): global cv,rect,font,pos,lst,step L = len(lst)-1 btn2.configure(state=tk.DISABLED) for i in range(L): for j in range(L-i): if lst[j]>lst[j+1]: lst[j],lst[j+1] = lst[j+1],lst[j] cv.move(rect[j],step,0) cv.move(rect[j+1],-step,0) rect[j],rect[j+1]=rect[j+1],rect[j] cv.itemconfig(font[j],text = str(lst[j]),fill='red') cv.itemconfig(font[j+1],text = str(lst[j+1]),fill='red') cv.itemconfig(rect[j],fill='orangered') cv.itemconfig(rect[j+1],fill='orangered') cv.update() sleep(0.4) cv.itemconfig(font[j],fill='black') cv.itemconfig(font[j+1],fill='black') cv.itemconfig(rect[j],fill='royalblue') cv.itemconfig(rect[j+1],fill='royalblue') cv.update() btn1.configure(state=tk.NORMAL) def main(): global cv,btn1,btn2 root = tk.Tk() root.geometry('640x480') root.title('Bubble Sort') root.resizable(False,False) cv = tk.Canvas(root, width=640, height=380, bg='aliceblue') cv.pack() btn1 = tk.Button(root,text='Create',command=init) btn1.place(x=240,y=420) btn2 = tk.Button(root,text='Bubble',command=bubble,state=tk.DISABLED) btn2.place(x=320,y=420) root.mainloop() if __name__=="__main__": app = main()
部分代碼注釋
給初次接觸 tkinter 控件的新同學(xué)給點(diǎn)代碼注釋?zhuān)罄袀兟赃^(guò):
1.root = tk.Tk()#Tkinter建立窗口 2.root.geometry('640x480') #設(shè)置分辨率 3.root.title('Bubble Sort') #設(shè)置窗口標(biāo)題 4.root.resizable(False,False) #取消窗口大小變動(dòng)
1.cv = tk.Canvas(root, width=640, height=380, bg='aliceblue') #創(chuàng)建畫(huà)面 2.cv.pack() # 控件布局方式: .pack自動(dòng)填充空間 .place指定位置 3.btn1 = tk.Button(root,text='Create',command=init) #創(chuàng)建按鈕 4.btn1.place(x=240,y=420) # place() 指定控件橫縱坐標(biāo)x,y 5.btn2 = tk.Button(root,text='Bubble',command=bubble,state=tk.DISABLED) 6.btn2.place(x=320,y=420) 7.# Canvas(): width height = 寬、高 bg=背景填充色 8.# Button(): text=按鈕標(biāo)題,command=綁定函數(shù),state=按鈕狀態(tài)
1.rect[i] = cv.create_rectangle(pos[i], fill='royalblue') #在畫(huà)布上創(chuàng)建矩形 2.font[i] = cv.create_text(x+i*step+7,y+10,text=str(lst[i]),anchor=tk.CENTER) 3.# create_text 參數(shù): X,Y坐標(biāo) ,text 文字, anchor=tk.CENTER 居中 4.btn2.configure(state=tk.NORMAL) #恢復(fù)btn2按鈕可點(diǎn)擊狀態(tài) 5.btn1.configure(state=tk.DISABLED) #設(shè)置btn2不可點(diǎn)擊
1.cv.move(rect[j+1],X,Y) #畫(huà)布子控件相對(duì)位置移動(dòng), X,Y正數(shù)向右或下,負(fù)數(shù)反向 2.cv.itemconfig(font[j],text = str(lst[j]),fill='red') #畫(huà)布子控件的參數(shù)設(shè)置文字顏色等
如果你想要其它功能,就可以考慮增加滑塊鈕聯(lián)動(dòng)count變量來(lái)改變初始化時(shí)色塊的數(shù)量,或者增加速度變量改變色塊交換的速度等等。常見(jiàn)排序有十種,基本上制作動(dòng)圖的原理是一樣的,只要知道排序的代碼就能做,開(kāi)動(dòng)起來(lái)自己動(dòng)手去制作吧!
---All done!
以上就是python編程冒泡排序法實(shí)現(xiàn)動(dòng)圖排序示例解析的詳細(xì)內(nèi)容,更多關(guān)于python冒泡排序動(dòng)圖實(shí)現(xiàn)的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。