The program visualizes the most common seven sorting algorithms using matplotlib
module's FuncAnimation
class.
For UI purposes I used tkinter
module, and another addition module random
.
Use the package manager pip to install tkinter, matplotlib and random.
pip install tkinter
pip install matplotlib
pip install random
You could use the program to understand how each algorithm works, or sorts a list!
After setup; RUN the code, you must see the following window,
Here you choose the size of array and sorting algorithm. After hit Animate.
This is the initial stage of visualization. Program will keep repeating the visualization until it is fully sorted.
This is the final stage and the program has completed the sorting!
The program is fully self-explanatory, one thing may stand out and that is yield
and yield from
.
FuncAnimation
class takes four required parameters.
Parameter | Description |
---|---|
fig | The figure object used to get needed events, such as draw or resize. |
func | The function to call at each frame. The first argument will be the next value in frames. |
frames | Source of data to pass func and each frame of the animation |
fargs | Additional arguments to pass to each call to func. |
It makes an animation by repeatedly calling a function func. In order to pass data to func and each frame, we need to use generator functions. Generator functions allow us to declare a function that behaves like an iterator, i.e. as for loop. Yield and yield methods are used to create a generator functions.
Here a function in my program, what is a example of generator function:
def selection_sort(list_):
for i in range(len(list_)):
lowest_value_index = i
yield list_
for j in range(i + 1, len(list_)):
if list_[j] < list_[lowest_value_index]:
lowest_value_index = j
yield list_
list_[i], list_[lowest_value_index] = list_[lowest_value_index], list_[i]
yield list_
The video in YouTube where I explained in details how the program works
Feedbacks I got from my sisters Google Drive