Selection sort visualizer using PyGame
In this article, we will see how to visualize Selection sort using a Python library PyGame. It is easy for the human brain to understand algorithms with the help of visualization. Selection sort is a simple and easy-to-understand algorithm that is used to sort elements of an array by dividing the array into two parts one sorted and the other unsorted part. Initially, the unsorted part contains all the elements and the sorted part is empty. In each iteration, we look for the minimum element in the unsorted part and swap it with the first element of the unsorted part and then we move the boundary of the sorted and unsorted parts one element to the right.
Selection sort visualizer using PyGame
We will be using PyGame, a Python library for game development. In the visualizer, there will be an array as a row of bars with varying heights and we will also use colors to differentiate between sorted and unsorted parts of the array.z
Necessary modules
pip install pygame
Steps for Selection Sort Visualizer in Pygame
Setting the Window Screen
First, we need to set up the PyGame environment and will create a window with a black background.
import pygame
import random
# Initialize PyGame
pygame.init()
# Set up the window
win_width = 1280
win_height = 720
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Selection Sort Visualizer GeekforGeeks")
win.fill((0, 0, 0))
Initialize the Sorted and Unsorted Part
Next, we need to generate a random array of integers and initialize the sorted and unsorted parts of the array
# Generate random array
arr_len = 50
arr = [random.randint(1, win_height) for i in range(arr_len)]
# Initialize sorted and unsorted parts of array
sorted_idx = -1
unsorted_idx = 0
Sorting Array Data
Now, we can create a loop that will iterate until the entire array is sorted
# Selection sort loop
while sorted_idx < arr_len - 1:
min_idx = unsorted_idx
for i in range(unsorted_idx, arr_len):
if arr[i] < arr[min_idx]:
min_idx = i
arr[unsorted_idx], arr[min_idx] = arr[min_idx], arr[unsorted_idx]
sorted_idx = unsorted_idx
unsorted_idx += 1
# Draw bars
win.fill((0, 0, 0))
for i in range(arr_len):
color = (255, 255, 255) if i <= sorted_idx else (255, 0, 0)
pygame.draw.rect(win, color,
(i * (win_width / arr_len), win_height - arr[i],
win_width / arr_len, arr[i]))
pygame.display.update()
Handle Exit of Code
Finally, we need to add some event handling to exit the program.
# Event handling loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Quit PyGame
pygame.quit()
Complete Code
Overall, your code will look something like this
import pygame
import random
# Initializing PyGame
pygame.init()
# Set up the window
win_width = 1280
win_height = 720
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Selection Sort Visualizer GeekforGeeks")
win.fill((0, 0, 0))
# Generating random array
arr_len = 50
arr = [random.randint(1, win_height) for i in range(arr_len)]
# Initializing sorted and unsorted parts of array
sorted_idx = -1
unsorted_idx = 0
# Selection sort loop
while sorted_idx < arr_len - 1:
min_idx = unsorted_idx
for i in range(unsorted_idx, arr_len):
if arr[i] < arr[min_idx]:
min_idx = i
arr[unsorted_idx], arr[min_idx] = arr[min_idx], arr[unsorted_idx]
sorted_idx = unsorted_idx
unsorted_idx += 1
# Drawing bars
delay = 100 # Change the value to adjust the delay in milliseconds
win.fill((0, 0, 0))
for i in range(arr_len):
color = (255, 255, 255) if i <= sorted_idx else (0, 255, 0)
pygame.draw.rect(win, color, (i * (win_width / arr_len),
win_height - arr[i], win_width / arr_len, arr[i]))
pygame.display.update()
pygame.time.wait(delay)
# Event handling loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Quit PyGame
pygame.quit()
Output:
