Skip to content

teo8192/Sorting-Visualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sorting visualizer

This is an algorithm visualizer written in python using pygame, that can visualize either python functions/methods or c functions.

Requirements

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

Running

./main.py

or

python3 main.py

A window will appear, visualizing the different sorting algorithms.

Adding your own sorting algorithm

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.

Implemented sorting algorithms

  • Radix sort
  • Heap sort
  • Merge sort
  • Quicksort
  • Selection sort
  • Insertion sort
  • Bubble sort

Changing the visualization function

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)

Other options

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.

Visualizing C functions

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.py

If 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.

About

A python script that visualizes different sorting algorithms (implemented either in pyhton or c)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published