Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Hangman Game in Python
Next article icon

In this article we will see how we can visualize thebubble sort algorithm using PyGame i.e when the pygame application get started we can see the unsorted bars with different heights and when we click space bar key it started getting arranging in bubble sort manner i.e after every iteration maximum value element should come at last.Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in form of an array with n number of elements. Bubble Sort compares all the element one by one and sort them based on their values.

Implementation steps : 1. Create a main window 2. Fill the main window with black color 3. Create a method to show the list of bar with specific gap in between them 4. Get the keys input from the user 5. If space bar is pressed start the sorting process 6. Implement bubble sort algorithm on the list 7. After every internal iteration fill the screen with black color and call the show method to show the iterated list in the form of bar.

Below is the implementation 

Python3
# importing pygameimportpygamepygame.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 heightdefshow(height):# loop to iterate each item of listforiinrange(len(height)):# drawing each bar with respective gappygame.draw.rect(win,(255,0,0),(x+30*i,y,width,height[i]))# infinite loopwhilerun:# execute flag to start sortingexecute=False# time delaypygame.time.delay(10)# getting keys pressedkeys=pygame.key.get_pressed()# iterating eventsforeventinpygame.event.get():# if event is to quitifevent.type==pygame.QUIT:# making run = false so break the while looprun=False# if space bar is pressedifkeys[pygame.K_SPACE]:# make execute flag to trueexecute=True# checking if execute flag is falseifexecute==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 techniqueforiinrange(len(height)-1):# after this iteration max element will come at lastforjinrange(len(height)-i-1):# starting is greater than next elementifheight[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 :


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp