Giter Site home page Giter Site logo

polcats / visualsort Goto Github PK

View Code? Open in Web Editor NEW
24.0 2.0 12.0 370 KB

An animated visualization of sorting algorithms.

Home Page: https://www.paulcatalan.dev/VisualSort/

HTML 40.22% JavaScript 43.55% CSS 16.23%
algorithms sorting-visualization algorithm-visualization html-animation javascript-animation

visualsort's Introduction

VisualSort

An animated visualization of sorting algorithms.

Algorithms

Animated

  • Bubble Sort
  • Comb Sort
  • Heap Sort
  • Insertion Sort
  • Selection Sort
  • Shell Sort

Implemented

  • Merge Sort

To Do

  • Counting Sort

How Does It Work

Animation Object

The animation object contains the frames which hold the indices of the elements to be highlighted and/or swapped.

The frames are essentially the stored "steps" of the algorithm.

animation = {
    "frames":[
        {
            "elements":[],
            "highlights":[0, 1]
        },
        {
            "elements":[0, 1],
            "highlights":[0, 1]
        }
        .
        .
    ]
}

Usage

The animation object is created in a sorting algorithm.
Particular events are stored as frames of the animation.

class Algorithms {
    static bubble(e, order) {
        let elements = e;
        let solution = new Animation();
        let swapped = false;

        for (let i = 0; i < elements.length; ++i) {
            swapped = false;
            for (let j = 0; j < elements.length - 1; ++j) {
                solution.addFrame(new Frame([], [j, j + 1])); // Record to-be-highlighted elements

                if (order == "desc" ? elements[j] < elements[j + 1] : elements[j] > elements[j + 1]) {
                    swapped = true;

                    const temp = elements[j];
                    elements[j] = elements[j + 1];
                    elements[j + 1] = temp;

                    solution.addFrame(new Frame([j, j + 1], [j, j + 1])); // Record to-be-swapped & to-be-highlighted elements
                }
            }

            if (!swapped) {
                break;
            }
        }
        return solution;
    }
}

Animating the Algorithm

The animation is played by a function that highlights the current elements in a frame and/or swaps them.

Target Changes

  • Refine Swap Animation
  • Algorithm Comparison
  • Custom Sort Order (Ascending or Descending)
  • Algorithm Details

References

visualsort's People

Contributors

celestria27 avatar polcats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.