2.3 opencv法:
2.3.1 来源:
#https://blog.csdn.net/u011983560/article/details/106195385
2.3.2 效果图:
![5种python方法实现冒泡排序可视化:Bubble Sort Visualizer](http://img.jiangsulong.com/220421/04531T624-4.jpg)
文章插图
2.3.3 代码四:
import numpy as npimport osimport cv2class Dataseq():WHITE = (255,255,255)RED = (0,0,255)BLACK = (0,0,0)YELLOW = (0,127,255)def __init__(self, data=https://www.isolves.com/it/cxkf/sf/2020-08-21/None,sort_type='figure'):self.sort_type=sort_typeself.interval=400#原来是5,太小了#注意与data数据最大值和最小值也有关系,与跳跃时间也有关self.inter=2if data==None:print("there are no data to sort!!!")os.exit()else:self.data=dataself.maxd=max(self.data)#最大值self.mind=min(self.data)#最小值self.getfigure()self.Visualize()self.sortdata()def getfigure(self):datanum=len(self.data)maxd=max(self.data)mind=min(self.data)self.figure=np.full((500*(maxd-mind)+50, (datanum)*(self.interval+self.inter), 3), 255,dtype=np.uint8)for i in range(len(self.data)):self.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.YELLOWdef choice(self, i, j):mind=self.mindself.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.BLACKself.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.BLACKself.Visualize()def change(self,i,j):mind=self.mindself.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.WHITEself.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.WHITEindata=self.data[i]self.data[i]=self.data[j]self.data[j]=indataself.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.BLACKself.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.BLACKself.Visualize()def Visualize(self):figure1=self.figurecv2.namedWindow('img',cv2.WINDOW_NORMAL)cv2.imshow('img',figure1)cv2.waitKey(1000)def sortdata(self):for di in range(len(self.data)):for dj in range(len(self.data)-di-1):self.choice(dj,dj+1)if self.data[dj]>self.data[dj+1]:self.change(dj,dj+1)self.getfigure()datat=Dataseq([10,20,30,15,25,18],'sort') datat.Visualize()cv2.destroyAllwindows()
2.4 pygame法:2.4.1 来源:
#https://github.com/zetaleee/Visualization-algorithm
2.4.2 效果图:![5种python方法实现冒泡排序可视化:Bubble Sort Visualizer](http://img.jiangsulong.com/220421/04531V1L-5.jpg)
文章插图
2.4.3 代码五:
import pygamefrom random import randintgap = 10#竖条的间隔width = 30#竖条的宽度screenSize = (600, 250)#显示屏幕的尺寸barXPosition = []#竖条在坐标轴的位置Bars = []#竖条对象列表#生成颜色class color(object):@staticmethoddef RandomColor():r,g,b = randint(0,225),randint(0,255),randint(0,255)return (r,g,b)@staticmethoddef CalculateColor(self,num):passclass bar(object):def __init__(self, n,num,screen,width = 30):self.n = nself.locationX = barXPosition[n]self.locationY = screenSize[1]-50-numself.num = numself.color = color.RandomColor()self.width = widthself.font = pygame.font.Font(None, 20)self.screen = screen#绘制竖条及其上方的数字def BarDraw(self):pygame.draw.rect(self.screen, self.color,((self.locationX,self.locationY), (self.width, self.num)))self.txt = self.font.render("{}".format(self.num), True, self.color)self.screen.blit(self.txt, (self.locationX+5,self.locationY-20))#移动竖条,flag是用于判断移动方向 True向右 False向左def move(self,flag):pace = 2#移动的步长#消除移动前的竖条pygame.draw.rect(self.screen, (255, 255, 235),((self.locationX, self.locationY), (self.width, self.num)))if flag:self.locationX += paceelse:self.locationX -= pace# 绘制移动后的竖条pygame.draw.rect(self.screen, self.color,((self.locationX , self.locationY), (self.width, self.num)))#交换相邻两个竖条def ChangeLocation(self,otherBall):#清除当前位置图像与文字pygame.draw.rect(self.screen, (255, 255, 235),((self.locationX, self.locationY-20), (self.width, self.num+20)))pygame.draw.rect(otherBall.screen, (255, 255, 235),((otherBall.locationX, otherBall.locationY - 20), (otherBall.width, otherBall.num + 20)))#竖条移动的动画for n in range(20):self.move(True)otherBall.move(False)pygame.time.delay(40)pygame.display.flip()#移动后,重新写上竖条对应的数字self.screen.blit(self.txt, (self.locationX + 5, self.locationY - 20))otherBall.screen.blit(otherBall.txt, (otherBall.locationX + 5, otherBall.locationY - 20))#交换竖条对象在列表的位置,同时交换排位数字Bars[self.n],Bars[otherBall.n] =Bars[otherBall.n],Bars[self.n]self.n,otherBall.n = otherBall.n,self.npygame.display.flip()pygame.time.delay(200)#此延时控制排序动画的快慢#冒泡排序def algorithm(nums):for i in range(len(nums) - 1):for j in range(len(nums) - 1 - i):if nums[j] > nums[j + 1]:Bars[j].ChangeLocation(Bars[j + 1])nums[j], nums[j + 1] = nums[j + 1], nums[j]#计算十二个竖条在轴上的位置def barX(gap,width,barXs):for n in range(12):barX = 50 + gap + (gap + width) * nbarXs.append(barX)def main():nums = []pygame.init()screen = pygame.display.set_mode(screenSize)pygame.display.set_caption("算法")#标题screen.fill((255, 255, 235))#背景色barX(gap,width,barXPosition)#计算bar位置并存于barXspygame.draw.aaline(screen,(0,255,0),(50,screenSize[1]-50),(screenSize[0]-50,screenSize[1]-50))#绘制坐标轴pygame.display.flip()#生成十二个竖条并绘制for n in range(12):num = randint(20,160)tempBar = bar(n,num,screen)tempBar.BarDraw()nums.append(num)Bars.append(tempBar)pygame.time.delay(50)#此处延时是为了开始时演示动画效果pygame.display.flip()algorithm(nums)#排序#等待关闭窗口事件run = Truewhile run:for event in pygame.event.get():if event.type == pygame.QUIT:run = Falseif __name__ == "__main__":main()
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CentOS6忘记root用户的密码重置方法
- python package自动更新
- 径山茶的储存方法,径山茶简介
- 四个春季中医养生保健方法 不可错过
- 春季养生有哪些方法?这些方法要谨记
- 春季预防颈椎病的几个方法
- 7个女性常见病的预防方法
- 最有益春季养生的几种方法
- 君山银针的制作流程,鉴定君山银针品质的方法
- 饵料|升温天气野钓思路,并非不好钓,方法很重要