This is an algorithm visualizer written in python using pygame, that can visualize either python functions/methods or c functions.
This works on Arch Linux running python 3.7.3 Other versions may work.
pygame 1.9.4 is used. Install by:
pip3 install pygame --user
./main.py
or
python3 main.py
A window will appear, visualizing the different sorting algorithms.
Write your algorithm in a function that takes two parameters: the data list and a function to draw said list.
Then in the main.py file, add the line
vis.visualize(your_algorithm)Spice up the algorithm with the drawing function to visualize it propperly.
- Radix sort
- Heap sort
- Merge sort
- Quicksort
- Selection sort
- Insertion sort
- Bubble sort
This is done by setting the mode property
from visualizer import Visualize
from sort import radixsort
vis = Visualize()
vis.mode = "bars"
vis.visualize(radixsort)The code above will visualize the radix sorting algorithm with bars
Currently implemented modes:
- rainbow (this is the default)
- grayscale
- boxes
- bars
- bars_ordered (bars are green if they are greater than or equal to the one before, red otherwise)
The window dimentions may be set with the dim variable when initializing Visualize
block_size is the with/height of the stripes in the rainbow, boxes and/or bars.
num is the number of stripes/bars etc.
It is also possible to set the step_through argument when initializing.
This will make the visualizer pause after each frame drawn, and will continue when the keyboard is pressed or the mouse is clicked.
Combined with showing important values (ex two values to be swapped or pivot choice in quicksort), the algorithm is better visualized.
Implement your c algorithm and add a call to it in the c_sort.c:sort() function.
Your sorting function needs to have the following signature:
void my_sort_func(int *data, int size, int(*drawfn)(int* data, int* important));To visualize a snapshot in the sorting, call the drawfn(data, NULL) function, with the data you wish to present as the only parameter.
If you have one or two specific values you want to highlight, you can call the function like this:
drawfn(data, (int[2]) { 1, 5 })This will draw data, but also highlight the values data[1] and data[5].
This important array needs to either be NULL or an array of size 2.
If the mode in the visualizer is set to either bars or boxes, the two walues with those indecies will be highlighted.
If you only want to highlight one value, set the other one to -1.
Run make to compile the shared library and run
python3 c_sort.pyIf you are on Windows, you may need to change the Makefile to generate a dynlib or dll something instead of a so. Then the c_sort.py file also needs to be changed to open the correct library.
If you want to change the looks of your visualization, go to the bottom of the c_sort.py file and customize the parameters of the SortC object.