Developer

Sorting Visualization

Visual representation of various sorting algorithms in action.

Timeframe
July 2021
Stack
Python3 · pygame · tkinter
  • Multiple sorting algorithms visualized
  • Real-time animation of sorting process
  • Educational tool for understanding algorithm behavior

Project Overview

This is a visualization of 4 sorting algorithms, commonly used to sort lists. This shows the difference in speed and space between each algorithm, really showing how much faster quicksort/the faster sorting algorithms are compared to other linear ones. I used pygame to make the GUI.

Time Complexity

Each sorting algorithm has its own complexity, determining the effectiveness of the algorithm. The quickest of the sorting algorithms used in the visualization, quicksort, has an average time complexity of O(n*log(n)) where n is the size of the input list.

For the slowest algorithm used in the visualization, bubblesort, the time complexity is O(n^2). This means that bubblesort is significantly slower than quicksort on average and does many more comparisons on average.

Implementation

To create this I made a sorting class which had attributes such as the list that needed to be sorted, the speed at which to sort it, the size of each bar, etc. All the sorting algorithms were put into this class and to display them onto the screen I used a method called paint() each time the sorting method would make a comparison. Most of the other code is just the basic pygame implementation with a few additions like the custom buttons that change color when pressed.

Here's the paint() method that renders the visualization:

def paint(self):
    pygame.event.pump()
    
    for i in range(len(self.list)):
        pygame.draw.rect(self.surface, self.color, pygame.Rect(
            self.x + (self.size+self.space) * i, 
            self.y-self.list[i], 
            self.size, 
            self.list[i]
        ))
    
    pygame.draw.line(self.surface, self.color, (40,500), (760,500), 1)
    comparisond = self.Font.render_to(
        self.surface, 
        (700,540), 
        str(self.iterations), 
        (255,255,255)
    )

And here's an example of the bubble sort implementation with visualization:

def bubble_sort(self):
    for i in range(len(self.list) - 1):
        for j in range(len(self.list) - i - 1):
            if self.list[j] > self.list[j + 1]:
                t = self.list[j]
                self.list[j] = self.list[j + 1]
                self.list[j + 1] = t
            self.surface.fill(self.surfColor)
            self.paint()
            self.highlight(j+1)
            
            if(len(self.list) > 75):
                pygame.time.delay(int(1))
            else:
                pygame.time.delay(int(self.speed))
            
            pygame.display.update()
            self.iterations += 1

The visualization updates in real-time as the algorithm makes comparisons, showing the iteration count and highlighting the current elements being compared.

All Sorting Algorithms on the same list

Bubble Sort

Quick Sort

Insertion Sort

Selection Sort

Future Plans

  • Add more sorting algorithms: merge sort, heap sort
  • Add more customization like buttons for changing the size of the list instead of using keyboard input