Bubble sort visualizer using PyGame

In this article we will see how we can visualize the bubble 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





# importing pygame
import pygame
# 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
    # 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
        # update the window
    # if execute flag is true
        # 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
                # create a time delay
                # update the display
# exiting the main window


Output :

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.