Skip to content

Python application to visualize sorting algorithms using RGB colours.

Notifications You must be signed in to change notification settings

Sklyvan/RainbowSorting

Repository files navigation

Array Visualizer 🍭

Python 3.7 software to visualize an array, using common sorting algorithms to visualize how this algorithms are working.

Installation

Use the package manager pip to install PyGame, NumPy and PyAudio

pip install pygame
pip install numpy
pip install pyaudio

Usage

With everything installed, open Main.py file with Python 3.7. If you want to change the array size, or the algorithm, go to Main.py file:

ARRAY_SIZE = # Select your size, max value is 1500.
AL = 1 # 1 for Insertion Sort, 2 for Cocktail Shaker Sort, 3 for Bubble Sort and 4 for Tim Sort.
SOUND = True # Choose if you want to hear the moves (Not working for Tim Sort).

Example Video

How to add more algorithms?

You just have to follow this pattern for any other algorithm that you want to implement. On Sorting_Algorithms.py

def Sorting_Algorithm(Array): # Example algorithm, without visualization.
    # Here goes sorting operations.
    return Array
    
def Sorting_Algorithm(MyArray, Win, Font):
    """
    Here goes sorting operations.
    Change every part where you are using Array, 
    and replace it by MyArray.Array. (Except return)
    Now every time that your algorithm access to the array, 
    add this code, to visualize the changes:
    """
    # Add to this list the elements that you are moving on this step.
    MyArray.Moving_Elements = [a, b, c, d...]
    KEY = KEY_PRESSED()
    if KEY == "QUIT":
        pygame.quit()
        sys.exit()
    else:
        MyArray.Draw(Win, Font)
    .
    .
    .
    .
    MyArray.isSorted = True
    return MyArray
    
# Finally, go to Main.py, and at line 8, add to it the name of your algoritm.
from Sorting_Algorithms import InsertionSort, CocktailShakerSort, BubbleSort, TimSort

About

Python application to visualize sorting algorithms using RGB colours.

Topics

Resources

Stars

Watchers

Forks

Languages