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 : Heap Sort
- Sorting Algorithm Visualization : Quick Sort
- Insertion Sort Visualization using Matplotlib in Python
- Sorting Algorithms Visualization : Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Bead Sort | A Natural Sorting Algorithm
- A sorting algorithm that slightly improves on selection sort
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Sorting objects using In-Place sorting algorithm
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Insertion sort to sort even and odd positioned elements in different orders
- Visualization of Merge sort using Matplotlib
- Merge Sort vs. Insertion Sort
- Insertion sort using C++ STL
- Insertion Sort
- Binary Insertion Sort
- Recursive Insertion Sort
- C Program for Insertion Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.