当前位置:网站首页>Using PyGame's Bubble Sort Visualizer
Using PyGame's Bubble Sort Visualizer
2022-08-08 16:05:00 【acktomas】
Using PyGame's Bubble Sort Visualizer
In this article, we'll see how to use PyGame to visualize the bubble-sort algorithm, which is when a pygame applicationOn startup, we can see unsorted bars with different heights, and when we click the spacebar, it starts to sort in a bubble sort fashion, i.e. at each iteration the max element should appear last.
Bubble Sort is a simple algorithm for sorting a given set of n elements, provided as an array of n elements.Bubble sort compares all elements one by one and sorts them according to their values.
Implementation steps:
- Create a main window
- Fill the main window with black
- Create a method to display a list of bars with a specific gap between them
- Get the key entered by the user
- If the space bar is pressed to start the sorting process
- Performs a bubble sort algorithm on a list
- After each inner iteration, fill the screen with black and call the show method to display the iteration list as a bar.
The following is the implementation
# importing pygameimport pygamepygame.init()# setting window sizewin = pygame.display.set_mode((500, 400))# setting title to the windowpygame.display.set_caption("Bubble sort")# initial positionx = 40y = 40# width of each barwidth = 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 heightdef show(height):# loop to iterate each item of listfor i in range(len(height)):# drawing each bar with respective gappygame.draw.rect(win, (255, 0, 0), (x + 30 * i, y, width, height[i]))# infinite loopwhile run:# execute flag to start sortingexecute = False# time delaypygame.time.delay(10)# getting keys pressedkeys = pygame.key.get_pressed()# iterating eventsfor event in pygame.event.get():# if event is to quitif event.type == pygame.QUIT:# making run = false so break the while looprun = False# if space bar is pressedif keys[pygame.K_SPACE]:# make execute flag to trueexecute = True# checking if execute flag is falseif execute == False:# fill the window with black colorwin.fill((0, 0, 0))# call the height method to show the list itemsshow(height)# update the windowpygame.display.update()# if execute flag is trueelse:# start sorting using bubble sort techniquefor i in range(len(height) - 1):# after this iteration max element will come at lastfor j in range(len(height) - i - 1):# starting is greater then next elementif height[j] > height[j + 1]:# save it in temporary variable# and swap them using temporary variablet = height[j]height[j] = height[j + 1]height[j + 1] = t# fill the window with black colorwin.fill((0, 0, 0))# call show method to display the list itemsshow(height)# create a time delaypygame.time.delay(50)# update the displaypygame.display.update()# exiting the main windowpygame.quit()
Output:
边栏推荐
猜你喜欢
微信公众号+web后台的工资条发放功能的实现
消除游戏中宝石下落的原理和实现
【云原生】-MySQL压测神器HammerDB的部署及使用
vs2只运行项目中的一个文件
【MATLAB项目实战】基于Morlet小波变换的滚动轴承故障特征提取研究
The origin and creation of Smobiler's complex controls
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
【软件工程之美 - 专栏笔记】40 | 最佳实践:小团队如何应用软件工程?
Nuxt - 网站接入 51LA 网站统计(详细教程)
Introduction to Power BI
随机推荐
hdu2475 Box
来,集中训练一下二叉树的层序遍历~
【云原生】云原生相关技术概念总结
大佬们,这个测试demo只能获取到全量数据,不能获取增量,我的mysql 已经开启了row模式的bi
hdu2475 Box
携手数字创新 共筑国产生态 7月份AntDB与5款产品完成互认证
2020年适用于Linux的10个顶级开源缓存工具
论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
EMQ畅谈IoT数据基础软件开源版图,引领本土开源走向全球
快速排序(C语言版)
Redis哨兵的配置和原理
全志V853芯片Tina下RTSP环境搭建方法
web-sql注入
Flutter的实现原理初探
国泰君安证券新手开户、有安全保障吗?
bzoj3262 Flowers bloom on Mo
Building and Visualizing Sudoku Games with Pygame
C语言学习概览(六)
【Unity入门计划】制作RubyAdventure02-处理瓦片地图&碰撞
文档管理系统:攻克这3个痛点,解决80%企业文档管理难题