免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2463 | 回复: 1

【分享】冒泡排序动态演示程序 [复制链接]

论坛徽章:
2
CU大牛徽章
日期:2013-04-17 11:46:28CU大牛徽章
日期:2013-04-17 11:46:39
发表于 2013-01-25 13:53 |显示全部楼层
本帖最后由 cdtits 于 2013-01-26 19:54 编辑

CSDN上某人做的,觉得不错,分享一下,望海涵!http://bbs.csdn.net/topics/390349402
QQ截图20130125135150.png
  1. '''
  2. Created on Jan 17, 2013

  3. @author: Shannon
  4. '''
  5. import wx
  6. import random


  7. size = 30
  8. xPos = 50
  9. BarData = []
  10. width = 900/(size*2)
  11. gap = 900/(size*4)
  12. delay = 100

  13. class AnimationPanel(wx.Panel):
  14.    
  15.     def __init__(self,parent,ID=-1,pos=(0,0),size=(750,500)):
  16.         wx.Panel.__init__(self,parent,ID,pos,size)
  17.         self.SetBackgroundColour("BLACK")
  18.         
  19.         self.GenerateBarData(xPos)
  20.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  21.          
  22.         self.font = wx.Font(8, wx.DEFAULT, wx.ITALIC, wx.NORMAL)
  23.         
  24.         self.timer = wx.Timer(self)
  25.         self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
  26.                
  27.   
  28.     #generate the original bars's data (x,y,w,h)   
  29.     def GenerateBarData(self,xPos):               
  30.         for num in range(size):           
  31.             xPos += (width + gap)
  32.             height = random.randrange(5,300)
  33.             barParameters=[xPos, 100, width, height]
  34.             BarData.append(barParameters)                  
  35.         self.RevertBarData()
  36.          
  37.     #revert the bars' yPos      
  38.     def RevertBarData(self):
  39.         maxH = self.maxHeight()
  40.         for h in range(size):            
  41.             moveH = maxH - BarData[h][3]
  42.             yPos = BarData[h][1]
  43.             yPos += moveH
  44.             BarData[h][1] = yPos
  45.   
  46.     #return the hightest height in bars
  47.     def maxHeight(self):
  48.         Height = []
  49.         #store all the bar height in Height[]
  50.         for h in range(size):
  51.             Height.append(BarData[h][3])
  52.         #find out the heightest bar and return
  53.         maxH = max(Height)
  54.         return maxH
  55.       
  56.     def OnPaint(self, event):
  57.         dc = wx.PaintDC(self)
  58.         self.paintBox(dc)
  59.       
  60.     def paintBox(self, dc):
  61.         dc.SetBrush(wx.Brush("green"))
  62.         dc.SetFont(self.font)
  63.         dc.SetTextForeground("GREEN")
  64.         for i in range(0,size):
  65.             dc.DrawRectangle(*BarData[i])
  66.             x = BarData[i][0]
  67.             y = BarData[i][1] + BarData[i][3]
  68.             h = str(BarData[i][3])
  69.             dc.DrawText(h, x, y)
  70.             
  71.                
  72.     #bubble sort
  73.     def BubbleSort(self):
  74.         for i in range(len(BarData), 1, -1):
  75.             for j in range(i-1):
  76.                 if BarData[j][3] > BarData[j+1][3]:
  77.                     BarData[j][1], BarData[j+1][1] = BarData[j+1][1], BarData[j][1]
  78.                     BarData[j][3], BarData[j+1][3] = BarData[j+1][3], BarData[j][3]
  79.                     yield
  80.                      
  81.     def OnTimer(self, event):
  82.         if not hasattr(self, 'g'):
  83.             self.g = self.BubbleSort()
  84.         try:
  85.             next(self.g)
  86.             self.Refresh()
  87.         except StopIteration:
  88.             del self.g
  89.             self.timer.Stop()
  90.             wx.MessageBox('Done!')
  91.         
  92. class SidePanel(wx.Panel):
  93.     def __init__(self, parent, aPanel, ID=-1,pos=(750,0),size=(250,500)):
  94.         wx.Panel.__init__(self,parent,ID,pos,size)
  95.         self.SetBackgroundColour("GREY")
  96.         self._aPanel = aPanel
  97.         
  98.         #---------------------------------------------        
  99.         #create Button_Panel on Side_Panel
  100.         self.Button_Panel = wx.Panel(self, -1,pos=(750,0),size=(250,150))
  101.         
  102.         #create ButtonSizer for button_panel
  103.         ButtonSizer = wx.GridSizer(rows = 2, cols = 2)
  104.         border = 3
  105.         #create 4 buttons label, add them to Button_Panel
  106.         #and binding them with corresponding event
  107.         buttonLabel = (("Start", self.OnStart),
  108.                        ("Step back", self.OnStepBack),
  109.                        ("Pause", self.OnPause),
  110.                        ("Step forward", self.OnStepForward))
  111.         
  112.         for eachLabel, eachHandle in buttonLabel:
  113.             button = wx.Button(self.Button_Panel, -1, eachLabel)
  114.             button.Bind(wx.EVT_BUTTON, eachHandle)
  115.             ButtonSizer.Add(button, 0, wx.EXPAND|wx.ALL, border)
  116.         self.Button_Panel.SetSizer(ButtonSizer)
  117.         
  118.         #---------------------------------------------

  119.         #---------------------------------------------
  120.         #Create ChoiceList_Panel for 2 choice dropdown list
  121.         #Sort_List: provide choice of sorting algorithm
  122.         #Graph_List: provide choice of graph algorithm
  123.         self.ChoiceList_Panel = wx.Panel(self, -1, pos=(750,150),size=(250,150))
  124.       
  125.         SortList = ['Bubble Sort','Insertion Sort','Merge Sort',
  126.                        'Quick Sort','Selection Sort','Shell Sort']
  127.         GraphList = ['Breath-First Search', 'Depth-First Search',
  128.                      'Dijkstra\'s Shortest Path', 'Prim\'s minimun spanning tree']
  129.         #wx.Choice(parent,id,pos,size,choices,style,validator,name)
  130.         SortChoice = wx.Choice(self.ChoiceList_Panel, -1,choices=SortList)
  131.         GraphChoice = wx.Choice(self.ChoiceList_Panel, -1, choices=GraphList)
  132.         al_tex = wx.StaticText(self.ChoiceList_Panel, -1, 'Select a sorting algorithm:')
  133.         graph_tex = wx.StaticText(self.ChoiceList_Panel, -1, 'Or a graph algorithm:')
  134.         ChoiceSizer = wx.BoxSizer(wx.VERTICAL)
  135.         ChoiceSizer.Add(al_tex,1,wx.EXPAND|wx.ALL)
  136.         ChoiceSizer.Add(SortChoice,1, wx.EXPAND|wx.ALL)
  137.         ChoiceSizer.Add(graph_tex,1,wx.EXPAND|wx.ALL)
  138.         ChoiceSizer.Add(GraphChoice,1,wx.EXPAND|wx.ALL)
  139.         self.ChoiceList_Panel.SetSizer(ChoiceSizer)
  140.         #---------------------------------------------

  141.         #---------------------------------------------
  142.         #create Size_Panel
  143.         #Provide a set of size RadioBox
  144.         self.Size_Panel = wx.Panel(self, -1, pos=(750,300),size=(250,100))
  145.         SizeList = ['5','10','15','20','25','30','35','40','45','50']
  146.         Size_RadioBox = wx.RadioBox(self.Size_Panel,-1,"Select size",wx.DefaultPosition,wx.DefaultSize,SizeList,5,wx.RA_SPECIFY_COLS)
  147.         SizeSizer = wx.BoxSizer(wx.VERTICAL)
  148.         SizeSizer.Add(Size_RadioBox,1, wx.EXPAND|wx.ALL)
  149.         self.Size_Panel.SetSizer(SizeSizer)
  150.         #---------------------------------------------

  151.         #---------------------------------------------
  152.         #create a Speed_Panel
  153.         #Speed_Panel contains a slider used for control the speed of animation
  154.         self.Speed_Panel = wx.Panel(self, -1, pos=(750,400),size=(250,100))
  155.         Speed_Slider = wx.Slider(self.Speed_Panel, -1,25,1,50,wx.DefaultPosition,(250,-1),wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS,name="Speed")
  156.         Speed_Slider.SetTickFreq(5,1)
  157.         Speed_tex = wx.StaticText(self.Speed_Panel, -1, "Change animation's speed")
  158.         Speed_Sizer = wx.BoxSizer(wx.VERTICAL)
  159.         Speed_Sizer.Add(Speed_tex,0,wx.EXPAND|wx.ALL)
  160.         Speed_Sizer.Add(Speed_Slider,0,wx.EXPAND|wx.ALL)
  161.         self.Speed_Panel.SetSizer(Speed_Sizer)
  162.         #---------------------------------------------
  163.         
  164.         Side_PanelSizer = wx.BoxSizer(wx.VERTICAL)
  165.         Side_PanelSizer.Add(self.Button_Panel,0,wx.EXPAND)
  166.         Side_PanelSizer.Add(self.ChoiceList_Panel,0,wx.EXPAND)
  167.         Side_PanelSizer.Add(self.Size_Panel,0,wx.EXPAND)
  168.         Side_PanelSizer.Add(self.Speed_Panel,0,wx.EXPAND)
  169.         self.SetSizer(Side_PanelSizer)
  170.         
  171.     def OnStart(self, event):
  172.         self._aPanel.timer.Start(delay)
  173.             
  174.     def OnStepBack(self, event):
  175.         pass

  176.     def OnPause(self, event):
  177.         self._aPanel.timer.Stop()

  178.     def OnStepForward(self, event):
  179.         pass
  180.             
  181. class MainFrame(wx.Frame):
  182.     def __init__(self):
  183.         
  184.         wx.Frame.__init__(self, None, title="GUI",size=(1000,500))

  185.         #---------------------------------------------      
  186.         #create 2 panels
  187.         #Animation_Panel---display the sorting animation
  188.         #Side_panels---display the buttons, comboBox, slider
  189.         self.Animation_Panel = AnimationPanel(self)
  190.         self.Side_Panel = SidePanel(self, self.Animation_Panel)      

  191.         #---------------------------------------------
  192.         #arrange the two main panels in the main sizer for the MainFrame
  193.         mainSizer = wx.BoxSizer(wx.HORIZONTAL)
  194.         mainSizer.Add(self.Animation_Panel, 2, wx.EXPAND)
  195.         mainSizer.Add(self.Side_Panel, 1, wx.EXPAND)
  196.         self.SetSizer(mainSizer)
  197.         mainSizer.Fit(self)
  198.         #---------------------------------------------
  199.         
  200.         
  201. if __name__ == '__main__':
  202.     app = wx.App()
  203.     frame = MainFrame()
  204.     frame.Show()
  205.     app.MainLoop()
复制代码

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
发表于 2013-01-26 17:43 |显示全部楼层
不容易啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP