3D Visualisation of Insertion Sort using Matplotlib in Python
Visualizing algorithms makes it easier to understand them by analyzing and comparing the number of operations that took place to compare and swap the elements. 3D visualization of algorithms is less common, for this we will use matplotlib to plot bar graphs and animate them to represent the elements of the array.
- We will generate an array with random elements.
- The algorithm will be called on that array and yield statement will be used instead of return statement for visualization purposes.
- We will yield the current states of the array after comparing and swapping. Hence the algorithm will return a generator object.
- Matplotlib animation will be used to visualize the comparing and swapping of the array.
- We will then plot the graph, which will return an object of Poly3dCollection using which further animation will be done.
Below is the implementation.