An algorithm like Heap sort can be understood easily by visualizing. In this article, a program that visualizes the Heap Sort Algorithm has been implemented.
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).
- Heapify the array to perform sorting.
- After Heapify, large bars are at the beginning followed by smaller bars.
- 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 particular time.
- Orange color highlights the bars sorted.
Press “Enter” key to Perform Visualization.
Press “R” key to generate new array.
After heapification of 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 : Insertion Sort
- Sorting Algorithms Visualization : Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Heap Sort for decreasing order using min heap
- 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
- Convert min Heap to max Heap
- Bead Sort | A Natural Sorting Algorithm
- A sorting algorithm that slightly improves on selection sort
- Insertion Sort Visualization using Matplotlib in Python
- Visualization of Quick sort using Matplotlib
- Visualization of Merge sort using Matplotlib
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Where is Heap Sort used practically?
- C++ Program for Heap Sort
- Java Program for Heap Sort
- Python Program for Heap 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.