python冒泡排序_5种python方法实现冒泡排序可视化:Bubble Sort Visualizer
1 說明:
=====
1.1 冒泡排序:
1.1.1 原理:比較兩個相鄰的元素,將值大的元素交換至右端。
1.1.2 來源:由來是因為越大的元素會經(jīng)由交換慢慢“浮”到數(shù)列的頂端,故得名。
1.1.3 是一種最簡單直觀的排序算法之一。
1.2 冒泡排序可視化:
1.2.1 Bubble Sort Visualizer。
1.2.2 更能直觀了解這種排序。
1.2.3 通過多種方法實現(xiàn),理解該算法,同時復習python。
1.2.4 python法,采用:matplotlib法、pygame法、turtle法(2d+3d)、opencv法。
1.2.5 代碼來源網(wǎng)絡,來源已注明,僅供學習,感謝網(wǎng)友,適合收藏。
============
具體代碼和講解
============
2 python法:
========
2.1 matplotlib法
2.1.1 效果圖:
2.1.2 代碼來源:
#https://blog.csdn.net/m1090760001/article/details/1032170522.1.3 代碼一:
from matplotlib import pyplot as pltimport randomLIST_SIZE = 10 #10個組barPAUSE_TIME = 4 / LIST_SIZE# 冒泡算法def bubble_sort(nums): for i in range(len(nums) - 1): for j in range(len(nums) - i - 1): if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] plt.cla() # 清除內(nèi)容 plt.bar(range(len(nums)), nums, align='center') plt.bar(j, nums[j], color="r", align="center") plt.bar(j + 1, nums[j + 1], color="r", align="center") plt.pause(PAUSE_TIME) plt.show()if __name__ == "__main__": nums = [] for i in range(LIST_SIZE): nums.append(random.randint(0, 1000)) #隨機生成 bubble_sort(nums) print(nums)2.2 turtle法:
2.2.1 2d-turtle法:
2.2.1.1 來源:
#https://blog.csdn.net/avskya/article/details/829589072.2.1.2 效果圖:
2.2.1.3 代碼二:
from turtle import *from random import randintfrom time import sleepclass Column(Turtle): def __init__(self,x): Turtle.__init__(self,shape='square') self.up() #抬筆 r = randint(0,255) g = randint(0,255) b = randint(0,255) self.fillcolor(r,g,b) self.scale = randint(1,38) # 20 * self.scale 為柱子高度 self.setx(x) self.__half__ = self.screen.window_height()//2 #屏幕的半高 self.shapesize(self.scale,1) #相對于自身坐標系前進的垂直方向放大 self.sety(0- self.__half__ + 10*self.scale + 30) if __name__=="__main__": width,height=800,800 screen = Screen() screen.colormode(255) screen.setup(width,height) screen.title("冒泡排序動態(tài)演示") screen.delay(0) xcors = [x for x in range(40-width//2,width//2-20,40) ] columns = [Column(x) for x in xcors ] #生成所有柱子 length = len(columns) while True: Flag = False #描述是否交換了數(shù)據(jù)的標志 for i in range(0,length-1 ): #由于越往后,越不要排這么多次數(shù)了. c1 = columns[i] #代表前面柱子 c2 = columns[i + 1] #代表后面柱子 if c1.scale > c2.scale: #如果前面的柱子更高,則交換位置 x1 = c1.xcor() #保留前面柱子c1的x坐標 x2 = c2.xcor() #保留后面柱子c2的x坐標 columns[i] = c2 #讓前面的柱子賦值為后面柱子 columns[i+1] = c1 #讓后面的柱子賦值為前面柱子 columns[i].setx(x1) #交換列表中項目后,重新設置x坐標 columns[i+1].setx(x2) #交換列表中項目后,重新設置x坐標 Flag = True screen.update() sleep(0.1) length = length - 1 if Flag == False:break #如果一次都沒有排序,則退出while循環(huán)2.2.2 3d-turtle法:
2.2.2.1 來源:
#https://blog.csdn.net/magicdoubi/article/details/888438252.2.2.2 效果圖:
2.2.2.3 代碼三:
from turtle import *from random import *import timedef bubble_sort(rectangle_list): for passnum in range(0, 10 - 1): for i in range(0, 10 - passnum - 1, 1): if rectangle_list[i].num > rectangle_list[i + 1].num: rectangle_list[i].replace(rectangle_list[i + 1]) time.sleep(0.5) # 休眠1秒class Cube(): def __init__(self, x, y, num, leng=10): self.x = x self.y = y self.num = num self.side_len = leng self.create_cube() def create_side(self): fillcolor("#639CD3") begin_fill() left(170) forward(self.side_len) right(80) forward(20 * self.num) right(100) forward(self.side_len) right(80) forward(20 * self.num) end_fill() setheading(0) # 恢復向右默認 def create_top(self): fillcolor("#95CEFF") penup() goto(self.x, self.y + 20 * self.num) pendown() begin_fill() forward(20) left(170) forward(self.side_len) left(10) forward(20) left(170) forward(self.side_len) end_fill() setheading(0) # 恢復向右默認 def create_rectangle(self): color("#639CD3") penup() goto(self.x, self.y) pendown() #柱的正面顏色 fillcolor("green") begin_fill() for x in range(1, 5): if x % 2 == 1: n = 20 else: n = 20 * self.num forward(n) left(90) end_fill() def create_cube(self): tracer(False) self.create_rectangle() self.create_side() self.create_top() tracer(True) def erase_rectangle(self): setheading(0) color("white") penup() goto(self.x, self.y) pendown() fillcolor("white") begin_fill() for x in range(1, 5): if x % 2 == 1: n = 20 else: n = 20 * self.num forward(n) left(90) end_fill() def erase_side(self): fillcolor("white") begin_fill() left(170) forward(self.side_len) right(80) forward(20 * self.num) right(100) forward(self.side_len) right(80) forward(20 * self.num) end_fill() setheading(0) # 恢復向右默認 def erase_top(self): fillcolor("white") penup() goto(self.x, self.y + 20 * self.num) pendown() begin_fill() forward(20) left(170) forward(self.side_len) left(10) forward(20) left(170) forward(self.side_len) end_fill() setheading(0) # 恢復向右默認 def erase_cube(self): tracer(False) self.erase_rectangle() self.erase_side() self.erase_top() tracer(True) def replace(self, n): self.erase_cube() n.erase_cube() self.num, n.num = n.num, self.num self.create_cube() n.create_cube()if __name__ == '__main__': hideturtle() var = list() for i in range(0, 10): var.append(Cube(35 * i, 0, randint(1, 10))) bubble_sort(var) done()2.3 opencv法:
2.3.1 來源:
#https://blog.csdn.net/u011983560/article/details/1061953852.3.2 效果圖:
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=None,sort_type='figure'): self.sort_type=sort_type self.interval=400 #原來是5,太小了 #注意與data數(shù)據(jù)最大值和最小值也有關系,與跳躍時間也有關 self.inter=2 if data==None: print("there are no data to sort!!!") os.exit() else: self.data=data self.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.YELLOW def choice(self, i, j): mind=self.mind self.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.BLACK self.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.BLACK self.Visualize() def change(self,i,j): mind=self.mind self.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.WHITE self.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.WHITE indata=self.data[i] self.data[i]=self.data[j] self.data[j]=indata self.figure[-50-(self.data[i]-mind)*500:,(i)*(self.interval+self.inter):(i)*(self.interval+self.inter)+self.interval]=self.BLACK self.figure[-50-(self.data[j]-mind)*500:,(j)*(self.interval+self.inter):(j)*(self.interval+self.inter)+self.interval]=self.BLACK self.Visualize() def Visualize(self): figure1=self.figure cv2.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-algorithm2.4.2 效果圖:
2.4.3 代碼五:
import pygamefrom random import randintgap = 10 #豎條的間隔width = 30 #豎條的寬度screenSize = (600, 250) #顯示屏幕的尺寸barXPosition = [] #豎條在坐標軸的位置Bars = [] #豎條對象列表#生成顏色class color(object): @staticmethod def RandomColor(): r,g,b = randint(0,225),randint(0,255),randint(0,255) return (r,g,b) @staticmethod def CalculateColor(self,num): passclass bar(object): def __init__(self, n,num,screen,width = 30): self.n = n self.locationX = barXPosition[n] self.locationY = screenSize[1]-50-num self.num = num self.color = color.RandomColor() self.width = width self.font = pygame.font.Font(None, 20) self.screen = screen #繪制豎條及其上方的數(shù)字 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 += pace else: 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() #移動后,重新寫上豎條對應的數(shù)字 self.screen.blit(self.txt, (self.locationX + 5, self.locationY - 20)) otherBall.screen.blit(otherBall.txt, (otherBall.locationX + 5, otherBall.locationY - 20)) #交換豎條對象在列表的位置,同時交換排位數(shù)字 Bars[self.n],Bars[otherBall.n] = Bars[otherBall.n],Bars[self.n] self.n,otherBall.n = otherBall.n,self.n pygame.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) * n barXs.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位置并存于barXs pygame.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 = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = Falseif __name__ == "__main__": main()===自己整理并分享出來===
喜歡的人,請點贊、關注、評論、轉(zhuǎn)發(fā)和收藏。
總結(jié)
以上是生活随笔為你收集整理的python冒泡排序_5种python方法实现冒泡排序可视化:Bubble Sort Visualizer的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 业余学python数据挖掘怎么赚钱_0基
- 下一篇: python 求均值_数据分析:寻找Py