Skip to content

negoro26/algorithm-visualizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Interactive Algorithm Visualizer

An educational desktop GUI application built with Python and Tkinter that visually demonstrates the step-by-step execution of sorting algorithms (Quicksort and Mergesort) and Binary Search.

Features

🎯 Core Functionality

  • Visual Algorithm Demonstration: Watch algorithms execute step-by-step with color-coded visualizations
  • Two Sorting Algorithms:
    • Quicksort: See pivot selection, partitioning, and recursive sorting
    • Mergesort: Observe array division and merging phases
  • Binary Search: Visualize the search space reduction in a sorted array
  • Interactive Controls: Play, pause, step-through, and reset animations
  • Customizable Arrays: Generate random arrays or input custom values

🎨 Visual Elements

  • Color-Coded States:
    • 🔵 Default (Blue): Unsorted elements
    • 🔴 Comparing (Red): Elements being compared
    • 🟡 Pivot (Orange): Pivot element in Quicksort
    • 🟢 Sorted (Green): Elements in final position
    • 💚 Found (Bright Green): Target found in Binary Search
    • 🟣 Searching (Purple): Current search range
    • 🟠 Merging/Partitioning: Active regions during sorting

📊 Educational Features

  • Algorithm Complexity Display: Shows time and space complexity for each algorithm
  • Step Counter: Track the number of operations performed
  • Status Updates: Detailed descriptions of each step
  • Speed Control: Adjustable animation speed (10ms to 2000ms)
  • Modern UI: Dark and light theme support with consistent padding and modern styling

Requirements

  • Python 3.8 or higher
  • Tkinter (included with Python)
  • sv-ttk (for modern theming) - pip install sv-ttk

Installation

  1. Clone or download this repository
  2. Ensure Python 3.8+ is installed on your system
  3. Install the required theme package:
    pip install sv-ttk

Usage

  1. Run the application:

    python main.py
  2. Generate or Load an Array:

    • Use the spinbox to set array size (5-50 elements)
    • Click "Generate Array" for random values
    • Or enter comma-separated values and click "Load Custom"
  3. Sorting Visualization:

    • Select algorithm (Quicksort or Mergesort) from dropdown
    • Click "Start Sorting"
    • Use Play/Pause button or Step button for control
    • Adjust speed with the slider
  4. Binary Search:

    • Ensure array is sorted (app will prompt if not, or sort first using the sorting algorithms)
    • After sorting is complete, binary search will be automatically enabled
    • Enter target value in the search field
    • Click "Start Binary Search"
    • Watch as the search space narrows
    • You can perform multiple searches on the same sorted array
  5. Animation Controls:

    • Play/Pause: Toggle continuous animation
    • Next Step: Execute one step at a time
    • Reset: Stop animation and return to initial state
    • Speed Slider: Control animation delay
    • Theme Toggle: Switch between dark and light themes

Project Structure

CautareBinara/
│
├── main.py           # Main application with GUI setup and event handling
├── algorithms.py     # Generator-based algorithm implementations
├── visualizer.py     # Canvas-based array visualization component
├── tests/            # Tests, debugging tools, and demonstration files
│   ├── test_algorithms.py    # Unit tests for algorithm correctness
│   ├── demo.py              # Interactive feature demonstration
│   ├── debug_visualizer.py  # Visualizer debugging tool
│   ├── test_array_issue.py  # Array size testing tool
│   └── README.md            # Documentation for test files
└── README.md        # This file

Algorithm Details

Quicksort

  • Implementation: Lomuto partition scheme
  • Visualization: Highlights pivot, shows partitioning process
  • Complexity: O(n log n) average, O(n²) worst case
  • Space: O(log n) for recursion stack

Mergesort

  • Implementation: Classic divide-and-conquer approach
  • Visualization: Shows division and merging phases
  • Complexity: O(n log n) always
  • Space: O(n) for temporary arrays

Binary Search

  • Implementation: Iterative approach
  • Visualization: Highlights search range and midpoint
  • Complexity: O(log n)
  • Space: O(1)

Code Architecture

The application follows a modular design:

  1. main.py:

    • Main application class (AlgorithmVisualizerApp)
    • GUI setup and event handling
    • Animation control logic
  2. algorithms.py:

    • Generator functions that yield algorithm states
    • Each state contains array snapshot and metadata
    • Enables smooth step-by-step visualization
  3. visualizer.py:

    • ArrayVisualizer class for canvas-based rendering
    • Handles bar drawing with dynamic scaling
    • Color management for different states

Educational Value

This visualizer helps students and developers:

  • Understand algorithm mechanics through visual representation
  • Compare algorithm performance with step counters
  • Learn complexity analysis with displayed Big O notation
  • Debug implementations by stepping through execution
  • Build intuition about how algorithms work on different inputs

Customization Options

  • Array Size: Adjustable from 5 to 50 elements
  • Value Range: Currently 1-100 (modifiable in code)
  • Animation Speed: 10ms to 2000ms per step
  • Theme: Toggle between dark and light themes
  • Color Scheme: Automatically adjusts based on selected theme
  • Consistent Padding: Standardized spacing throughout the interface

Testing

The project includes comprehensive tests and debugging tools in the tests/ folder:

  • Run Unit Tests: cd tests && python test_algorithms.py
  • View Feature Demo: cd tests && python demo.py
  • Debug Visualization: cd tests && python debug_visualizer.py

See tests/README.md for detailed information about each test file.

Tips for Learning

  1. Start Small: Use arrays of 5-10 elements to understand the basics
  2. Step Through: Use the "Next Step" button to follow each operation
  3. Compare Algorithms: Try the same array with different sorting algorithms
  4. Edge Cases: Test with already sorted, reverse sorted, and random arrays
  5. Binary Search Practice: Try finding different values to understand the search pattern

Future Enhancements (Optional)

Potential improvements for extended learning:

  • Additional algorithms (Bubble Sort, Heap Sort, etc.)
  • Algorithm comparison mode (side-by-side)
  • Performance statistics and graphs
  • Export animation as GIF
  • Sound effects for operations
  • More detailed pseudocode display

License

This project is created for educational purposes. Feel free to use, modify, and distribute.

Troubleshooting

  • Window doesn't appear: Ensure Tkinter is properly installed with Python
  • Animations too fast/slow: Adjust the speed slider
  • Array too large: Maximum size is 50 for optimal visualization
  • Binary Search not working: Array must be sorted first

Note: This visualizer is designed for learning and understanding algorithms. The implementations prioritize clarity and visualization over performance optimization.

About

GUI App to visualize algorithms for study

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages