5种python方法实现冒泡排序可视化:Bubble Sort Visualizer

1 说明:
=====
1.1 冒泡排序:
1.1.1 原理:比较两个相邻的元素,将值大的元素交换至右端 。
1.1.2 来源:由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故得名 。
1.1.3 是一种最简单直观的排序算法之一 。

5种python方法实现冒泡排序可视化:Bubble Sort Visualizer

文章插图
 
1.2 冒泡排序可视化:
1.2.1 Bubble Sort Visualizer 。
1.2.2 更能直观了解这种排序 。
1.2.3 通过多种方法实现,理解该算法,同时复习Python 。
1.2.4 python法,采用:matplotlib法、pygame法、turtle法(2d+3d)、opencv法 。
1.2.5 代码来源网络,来源已注明,仅供学习,感谢网友,适合收藏 。
============
具体代码和讲解
============
2 python法:
========
2.1 matplotlib法
2.1.1 效果图:
5种python方法实现冒泡排序可视化:Bubble Sort Visualizer

文章插图
 
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()# 清除内容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 效果图:
5种python方法实现冒泡排序可视化:Bubble Sort Visualizer

文章插图
 
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,800screen = Screen()screen.colormode(255)screen.setup(width,height)screen.title("冒泡排序动态演示")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#描述是否交换了数据的标志for i in range(0,length-1 ):#由于越往后,越不要排这么多次数了.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 = Truescreen.update()sleep(0.1)length = length - 1ifFlag == False:break #如果一次都没有排序,则退出while循环2.2.2 3d-turtle法:
2.2.2.1 来源:
#https://blog.csdn.net/magicdoubi/article/details/888438252.2.2.2 效果图:
5种python方法实现冒泡排序可视化:Bubble Sort Visualizer

文章插图
 
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 = xself.y = yself.num = numself.side_len = lengself.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 = 20else:n = 20 * self.numforward(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 = 20else:n = 20 * self.numforward(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.numself.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()


推荐阅读