【PyQt】アルゴリズム-挿入、再帰、泡立ち
16245 ワード
# 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の二乗