【PyQt】算法-插入、递归、冒泡
# coding=utf-8 import sys from PyQt4.QtGui import * from PyQt4.QtCore import * class MainWindow(QMainWindow): def __init__(self,parent=None): super(MainWindow,self).__init__(parent) self.setWindowTitle(u'算法实现') self.setWindowIcon(QIcon('image/5.png')) #设置输入数据窗口&数据输出窗口 in_out_putDock=QDockWidget(u'输入输出') in_out_putDock_workspace=QWorkspace() in_out_putDock.setWidget(in_out_putDock_workspace) label_in=QLabel(u'输入数据窗口,数据以“,”分隔') label_out=QLabel(u'输出数据窗口') self.inTextEdit=QTextEdit() self.outTextEdit=QTextEdit() layout=QGridLayout() layout.addWidget(label_in,0,0) layout.addWidget(self.inTextEdit,1,0,1,4) layout.addWidget(label_out,2,0) layout.addWidget(self.outTextEdit,3,0,1,4) in_out_putDock_workspace.setLayout(layout) self.addDockWidget(Qt.LeftDockWidgetArea,in_out_putDock) #设置算法(algorithm)按钮及运行时间显示控件 algori_Dock=QDockWidget(u'各类算法') algori_Dock_workspace=QWorkspace() algori_Dock.setWidget(algori_Dock_workspace) self.clearButton=QPushButton(u'清除输出框内容') self.clear_in_Button=QPushButton(u'清除输入框内容') self.insert_sort_Button=QPushButton(u'插入升序算法') self.merge_Button=QPushButton(u'分治递归升序算法') self.bubble_Button=QPushButton(u'冒泡升序算法') time_label=QLabel(u'算法运行时间: ') self.runTime_label=QLabel() self.runTime_label.setFrameShape(QFrame.Box) layout2=QGridLayout() layout2.addWidget(time_label,0,0) layout2.addWidget(self.runTime_label,0,1) layout2.addWidget(self.clearButton,1,0,1,1) layout2.addWidget(self.clear_in_Button,1,1,1,1) layout2.addWidget(self.insert_sort_Button,2,0,1,1) layout2.addWidget(self.merge_Button,2,1,1,1) layout2.addWidget(self.bubble_Button,3,0,1,1) layout2.setRowStretch(4,1) algori_Dock_workspace.setLayout(layout2) self.addDockWidget(Qt.RightDockWidgetArea,algori_Dock) #将按钮与动作相连 self.connect(self.insert_sort_Button,SIGNAL('clicked()'),self.insertSortUp) self.connect(self.clearButton,SIGNAL('clicked()'),self.clearInOutEdit) self.connect(self.merge_Button,SIGNAL('clicked()'),self.MergeSortUp) self.connect(self.clear_in_Button,SIGNAL('clicked()'),self.clearInEdit) self.connect(self.bubble_Button,SIGNAL('clicked()'),self.bubbleSortUp) #从输入文本框中获取文本并返回数字列表 def getData(self): textData=self.inTextEdit.document() textData_str=textData.toPlainText() data=str(textData_str).split(',') data_f=[] for i in range(len(data)): data_f.append(float(data[i])) return data_f def showResult(self,pname,runcount,time,data): tex=pname+u': ||运行时间为:'+str(time)+u' 毫秒 ||运行所用循环执行次数为:'+str(runcount)+u' 次||运行结果为: '+str(data) self.outTextEdit.append(tex) self.runTime_label.setText(str(time)+u'毫秒') def insertSortUp(self): run_time=QTime() run_time.start() data=self.getData() runcount=0 #插入排序算法 j=1 for j in range(len(data)): key=data[j] i=j-1 while i>=0 and data[i]>key: data[i+1]=data[i] i-=1 runcount+=1 data[i+1]=key spend_time=run_time.elapsed() self.showResult(u'插入升序算法',runcount,spend_time,data) def clearInOutEdit(self): self.outTextEdit.clear() def clearInEdit(self): self.inTextEdit.clear() #分治递归合并两个数组 def Merge(self,data,p,q,r,runcount): n1=q-p+1 n2=r-q L=[] R=[] for i in range(n1): L.append(data[p+i-1]) runcount+=1 for j in range(n2): R.append(data[q+j]) runcount+=1 L.append(1000000000) R.append(1000000000) a=0 b=0 k=p #self.outTextEdit.append(str(L)+'+'+str(R)) while k<=r: if L[a]<R[b]: data[k-1]=L[a] a+=1 else: data[k-1]=R[b] b+=1 k+=1 runcount+=1 #递归主函数 def MergeSort(self,data,p,r,runcount): if p<r: q=int((p+r)/2) self.MergeSort(data,p,q,runcount) self.MergeSort(data,q+1,r,runcount) self.Merge(data,p,q,r,runcount) runcount+=1 #递归按钮动作 def MergeSortUp(self): run_time1=QTime() run_time1.start() data=self.getData() runcount=0 self.MergeSort(data,1,len(data),runcount) spend_time=run_time1.elapsed() self.showResult(u'分治递归升序算法',runcount,spend_time,data) #冒泡排序算法 def bubbleSortUp(self): run_time_bubble=QTime() run_time_bubble.start() data=self.getData() runcount=0 for i in range(len(data)): j=len(data)-1 while j>i: if data[j]<data[j-1]: temp=data[j] data[j]=data[j-1] data[j-1]=temp runcount+=1 j-=1 spend_time_bubble=run_time_bubble.elapsed() self.showResult(u'冒泡升序排序算法',runcount,spend_time_bubble,data) app=QApplication(sys.argv) form=MainWindow() form.show() app.exec_()
插入算法的数量级为n的平方 递归数量级为logn 冒泡排序的数量级为n的平方