An algorithm like Insertion Sort can be understood easily by visualizing. In this article, a program that visualizes the Insertion Sort Algorithm has been implemented.
The Graphical User Interface(GUI) is implemented in python using pygame library.
- Generate random array and fill the pygame window with bars. Bars are straight vertical lines, which represents array elements.
- Set all bars to green color (unsorted).
- Use pygame.time.delay() to slow down the algorithm, so that we can see the sorting process.
- Implement a timer to see how the algorithm performs.
- The actions are performed using ‘pygame.event.get()’ method, which stores all the events which user performs, such as start, reset.
- Blue color is used to highlight bars that are involved in sorting at a paticular time.
- Orange color highlights the bars sorted.
We can clearly see from the Insertion Sort visualization, that insertion Sort is very slow compared to other sorting algorithms like Mergesort or Quicksort.
Press “Enter” key to Perform Visualization.
Press “R” key to generate new array.
Please make sure to install the pygame library before running the below program.
Below is the implementation of the above visualizer:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sorting Algorithm Visualization : Merge Sort
- Sorting Algorithm Visualization : Quick Sort
- Sorting algorithm visualization : Heap Sort
- Insertion Sort Visualization using Matplotlib in Python
- Sorting Algorithms Visualization : Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting objects using In-Place sorting algorithm
- Insertion sort to sort even and odd positioned elements in different orders
- Merge Sort vs. Insertion Sort
- Bead Sort | A Natural Sorting Algorithm
- A sorting algorithm that slightly improves on selection sort
- Visualization of Quick sort using Matplotlib
- Visualization of Merge sort using Matplotlib
- Time complexity of insertion sort when there are O(n) inversions?
- An Insertion Sort time complexity question
- Recursive Insertion Sort
- Insertion Sort