用python实现,冒泡排序演示
1 # -*- coding:utf-8 -*- 2 import time 3 from Tkinter import * 4 a=[1,9,5,6,8,1] 5 6 class CanvasDemo(): 7 #n1=70 8 #n2=n1+40 9 d1=390 10 d2=d1-60 11 weiyi=len(a)-1 12 def __init__(self): 13 14 n1=70 15 n2=n1+40 16 window=Tk() 17 window.title("冒泡排序演示") 18 self.canvas=Canvas(window,width=500,height=400,bg='white')##设置画布大小,颜色 19 self.canvas.pack() 20 frame=Frame(window) 21 frame.pack() 22 ##self.paixu() 23 self.displayRect() 24 self.n1=n1+60 25 self.n2=n2+60 26 btString=Button(frame,text="执行",command=self.paixu) 27 btString.grid(row=1,column=6) 28 window.mainloop() 29 30 31 def displayRect(self):##打印表格 32 self.canvas.create_rectangle(60,30,420,90,tags="rect")##左上右下 33 self.canvas.create_rectangle(120,30,360,90,tags="rect") 34 self.canvas.create_rectangle(180,30,300,90,tags="rect") 35 self.canvas.create_rectangle(240,30,240,90, tags="rect") 36 def paixu(self): 37 38 for i in range(0,len(a)): 39 j=len(a)-1##添加交换之前 40 self.weiyi=len(a)-1 41 while j>i: 42 self.displayString2() 43 time.sleep(1) 44 #if j>i: 45 # self.canvas.delete("int") 46 47 ##self.weiyi=len(a)-1 48 self.displayString3() 49 time.sleep(1) 50 if a[j-1]>a[j]: 51 a[j-1],a[j]=a[j],a[j-1] 52 #j=j-1 53 self.displayString4() 54 if j>=i: 55 self.canvas.delete("int") 56 self.displayString1()##打印交换之后的 57 time.sleep(1) 58 59 if j>=i: 60 self.canvas.delete("int") 61 j=j-1 62 self.displayString1() 63 64 65 66 def displayLine(self):## 设置箭头指向 67 self.canvas.create_line(self.n2,30,self.n2,10,self.n2+60,10,self.n2+60,30,width=1,arrow="last",tags="line") 68 69 def displayString1(self):## 打印序列 70 n1=90 71 ##time.sleep(1) 72 73 for z in range(len(a)): 74 self.canvas.create_text(n1,60,text="%s"%a[z],font="Times 20 bold ",tags="int") 75 n1=n1+60 76 self.canvas.update() 77 #self.update() 78 #time.sleep(0.5) 79 80 def displayString2(self): 81 n2=90 82 for z in range(len(a)): 83 if z!=self.weiyi and z!=self.weiyi-1: 84 self.canvas.create_text(n2,60,text="%s"%a[z],font="Times 20 bold ",tags="int") 85 n2=n2+60 86 self.canvas.update() 87 self.weiyi=self.weiyi-1 88 def displayString3(self): 89 n2=90 90 for z in range(len(a)): 91 if z==self.weiyi: 92 if a[z]>a[z+1]: 93 self.canvas.create_line(n2,30,n2,10,n2+60,10,n2+60,30,width=1,arrow="last",tags="line") 94 self.canvas.create_text(n2,60,text="%s"%a[z],font="Times 20 bold ",tags="int1") 95 self.canvas.create_text(n2+60,60,text="%s"%a[z+1],font="Times 20 bold",tags="int1") 96 for x in range(0,10): 97 self.canvas.move("int1",0,20) 98 self.canvas.move("int12",0,20) 99 self.canvas.update() 100 time.sleep(0.1) 101 if(x==9): 102 self.canvas.delete("int1") 103 n2=n2+60 104 self.canvas.update() 105 def displayString4(self): 106 n2=90 107 for z in range(len(a)): 108 if z==self.weiyi: 109 self.canvas.create_text(n2,300,text="%s"%a[z],font="Times 20 bold ",tags="int2") 110 self.canvas.create_text(n2+60,300,text="%s"%a[z+1],font="Times 20 bold ",tags="int2") 111 for x in range(0,10): 112 self.canvas.move("int2",0,-20) 113 self.canvas.update() 114 time.sleep(0.1) 115 if(x==9): 116 self.canvas.delete("int2","line") 117 n2=n2+60 118 119 CanvasDemo()