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. For this we will use matplotlib, to plot bar graphs 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 a 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.
- The array will be stored in a matplotlib bar container object (‘rects’), where the size of each bar will be equal to the corresponding value of the element in the array.
- The inbuilt FuncAnimation method of matplotlib animation will pass the container and generator objects to the function used to create animation. Each frame of the animation corresponds to a single iteration of the generator.
- The animation function is repeatedly called will set the height of the rectangle equal to the value of the elements.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.