Giter Site home page Giter Site logo

paoloose / sorthem Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 2.11 MB

A language agnostic tool to visualize your sorting algorithms easily

Shell 2.00% C++ 82.00% JavaScript 3.05% Python 3.83% C 4.11% Ruby 5.00%
learning-tools sorting-algorithm-visualizations sorting-algorithms sorting-visualization

sorthem's Introduction

Sorthem

Sorthem is a language-agnostic tool that enables you to visualize your sorting algorithms with just a few lines of code.

How does it work?

Sorthem is simple. You have a sorting algorithm, written in whatever language you like. Let's say, a bubble sort implementation in Ruby:

arr = [3, 9, 1, 8, 6, 10, 4, 7, 2, 5]

for i in 0..arr.length - 2 do
    for j in 0..arr.length - i - 2 do
        left = arr[j]
        right = arr[j + 1]

        if left > right
            arr[j], arr[j + 1] = right, left
        end
    end
end

Almost all algorithms are made up with basic operations like comparing two values, swapping them, etc.

Sorthem needs to know two things:

  • what are the values you're sorting
  • what are the operations you're doing in order to sort those values

An all you need to do is to print that information to the standard output.

arr = [3, 9, 1, 8, 6, 10, 4, 7, 2, 5]

# Print the array in the format [ 3 9 1 8 6 10 4 7 2 5 ]
puts "[ #{arr.join(' ')} ]"

for i in 0..arr.length - 2 do
    for j in 0..arr.length - i - 2 do
        left = arr[j]
        right = arr[j + 1]

        # Tell Sorthem that you're comparing two values
        puts "compare #{j} #{j + 1}"
        if left > right
            # Tell Sorthem that you're swapping two values
            puts "swap #{j} #{j + 1}"
            arr[j], arr[j + 1] = right, left
        end
    end
end

With these three lines of code, you now have a visualizer for your algorithm. You can run it with:

ruby bubble_sort.rb | sorthem

Bubble Sort sorthem demo

Note that compare isn't actually needed. Only swap does the job of sorting the array. compare is simply a helpful operation for visualization.

Of course, there are more types of operations for more complex algorithms, each associated with semantic color.

You have a variety of examples in the examples directory (WIP).

Basic operations

  • compare: for comparing to indexes

    usage: compare i j

  • swap: for swapping the values of two indexes

    usage: swap i j

  • get: for consulting the value at some index

    usage: get i

  • set: for setting a value at some index

    usage: set i value

    where 0 <= value <= max_value, otherwise panics

  • context: makes future index ops relative to this context

    usage: context start end

    where startand end fall within the array's bounds

  • pop: pops the last context

    usage: pop

    panics if there are no contexts to pop

i and j must be valid zero-based indexes.

Getting started

Usage

your_sorting_algorithm | sorthem

Controls

  • Space - pause/resume
  • R - restart
  • โ†‘ - speed up
  • โ†“ - speed down

Building sorthem

sorthem uses SFML for graphics. The instalation process depends on your OS. See the official documentation.

# Debian based distros
sudo apt update
sudo apt install libsfml-dev

# On MacOS
brew install sfml

There are no official packages for Windows, but you can download the binaries from the official website.

Run the following commands to build sorthem:

git clone https://github.com/paoloose/sorthem.git
cd sorthem

# On windows, build under WSL
./build.sh
cat examples/test | dist/sorthem

Semantic Colors

  • #ffffff #ffffff: an iddle bar
  • #e7ccff #ffffff: iddle bar within a context
  • #f33232 #f33232: two bars being swaped
  • #9b54c3 #9b54c3: getting the value of the bar
  • #2d43db #2d43db: setting the value of the bar
  • #e79933 #e79933: two bars are being compared

sorthem's People

Contributors

paoloose avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

michipache21

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.