当前位置:网站首页>使用 PyGame 的冒泡排序可视化工具
使用 PyGame 的冒泡排序可视化工具
2022-08-08 16:00:00 【acktomas】
使用 PyGame 的冒泡排序可视化工具
在本文中,我们将看到如何使用 PyGame 可视化冒泡排序算法,即当 pygame 应用程序启动时,我们可以看到具有不同高度的未排序条,当我们单击空格键时,它开始以冒泡排序方式排列,即在每次迭代最大值元素应该最后出现。
冒泡排序是一种简单的算法,用于对一组给定的 n 个元素进行排序,这些元素以具有 n 个元素的数组的形式提供。冒泡排序将所有元素一一比较,并根据它们的值对它们进行排序。
实施步骤:
- 创建一个主窗口
- 用黑色填充主窗口
- 创建一个方法来显示在它们之间有特定间隙的条形列表
- 获取用户输入的键
- 如果按下空格键启动排序过程
- 对列表执行冒泡排序算法
- 每次内部迭代后,用黑色填充屏幕并调用 show 方法以条形的形式显示迭代列表。
下面是实现
# importing pygame
import pygame
pygame.init()
# setting window size
win = pygame.display.set_mode((500, 400))
# setting title to the window
pygame.display.set_caption("Bubble sort")
# initial position
x = 40
y = 40
# width of each bar
width = 20
# height of each bar (data to be sorted)
height = [200, 50, 130, 90, 250, 61, 110,
88, 33, 80, 70, 159, 180, 20]
run = True
# method to show the list of height
def show(height):
# loop to iterate each item of list
for i in range(len(height)):
# drawing each bar with respective gap
pygame.draw.rect(win, (255, 0, 0), (x + 30 * i, y, width, height[i]))
# infinite loop
while run:
# execute flag to start sorting
execute = False
# time delay
pygame.time.delay(10)
# getting keys pressed
keys = pygame.key.get_pressed()
# iterating events
for event in pygame.event.get():
# if event is to quit
if event.type == pygame.QUIT:
# making run = false so break the while loop
run = False
# if space bar is pressed
if keys[pygame.K_SPACE]:
# make execute flag to true
execute = True
# checking if execute flag is false
if execute == False:
# fill the window with black color
win.fill((0, 0, 0))
# call the height method to show the list items
show(height)
# update the window
pygame.display.update()
# if execute flag is true
else:
# start sorting using bubble sort technique
for i in range(len(height) - 1):
# after this iteration max element will come at last
for j in range(len(height) - i - 1):
# starting is greater then next element
if height[j] > height[j + 1]:
# save it in temporary variable
# and swap them using temporary variable
t = height[j]
height[j] = height[j + 1]
height[j + 1] = t
# fill the window with black color
win.fill((0, 0, 0))
# call show method to display the list items
show(height)
# create a time delay
pygame.time.delay(50)
# update the display
pygame.display.update()
# exiting the main window
pygame.quit()
输出 :
边栏推荐
- Dry goods: design high concurrency architecture from scratch
- The origin and creation of Smobiler's complex controls
- 京东T9纯手打688页神笔记,SSM框架整合Redis搭建高效互联网应用
- 本博客目录及版权申明
- redis的详细介绍与操作命令
- [Unity entry plan] Unity instance - how to protect data members through encapsulation in C#
- 【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
- 如何制作网页
- 科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
- 基于Qt设计的课堂考勤系统(采用RDS for MySQL云数据库 )【华为云至简致远】
猜你喜欢
解决Redis、MySQL缓存双写不一致问题
成员变量和局部变量的区别?
Flutter的实现原理初探
【对线面试官】如何实现去重和幂等
分享这些2022设计师们决不能错过的Blender新插件
Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
vs2只运行项目中的一个文件
Superset 1.2.0 安装
彻底理解 volatile 关键字及应用场景,面试必问,小白都能看懂!
科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
随机推荐
mmdetection最新版食用教程(一):安装并运行demo及开始训练coco
bzoj3693 round table hall theorem + segment tree
groovy基础学习
Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
[Unity Starter Plan] Making RubyAdventure02 - Handling Tile Maps & Collision
web-sql注入
携手数字创新 共筑国产生态 7月份AntDB与5款产品完成互认证
基于FTP协议的Excel文件上传与下载
快速排序(C语言版)
2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓
如何选择ui设计机构
Metamask插件中-添加网络和切换网络
pytorch安装过程中出现torch.cuda.isavailable()=False问题
The origin and creation of Smobiler's complex controls
广东大学生网络安全攻防大赛CTF部分WP
10分钟快速入门RDS【华为云至简致远】
hdu2475 Box
A16z:为什么 NFT 创作者要选择 cc0?
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】
PayPal无差别封号潮,被围剿的站群模式还能玩多久?如何避免shopify封店