8000 GitHub - cathoderay/algo-engine: Engine to study and visualize algorithms
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cathoderay/algo-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

algo-engine

Engine to study and visualize algorithms. No AI was used in this project.

engine

In order to study an algorithm, you just need to instantiate an engine and call the algorithm you want.

Below, you can see an example for the insertion sort.

  from engine import Engine
  import pprint

  engine = Engine()
  engine.insertion_sort([4, 2, 3, 1])
  pprint.pprint(engine.get_events())
[
   {'name': 'initial', 'state': (4, 2, 3, 1), 'indices': (None, None), 'swaps': 0, 'comparisons': 0},
   {'name': 'compare', 'state': (4, 2, 3, 1), 'indices': (1, 0), 'swaps': 0, 'comparisons': 1},
   {'name': 'swap', 'state': (2, 4, 3, 1), 'indices': (1, 0), 'swaps': 1, 'comparisons': 1},
   {'name': 'compare', 'state': (2, 4, 3, 1), 'indices': (2, 1), 'swaps': 1, 'comparisons': 2},
   {'name': 'swap', 'state': (2, 3, 4, 1), 'indices': (2, 1), 'swaps': 2, 'comparisons': 2},
   {'name': 'compare', 'state': (2, 3, 4, 1), 'indices': (1, 0), 'swaps': 2, 'comparisons': 3},
   {'name': 'compare', 'state': (2, 3, 4, 1), 'indices': (3, 2), 'swaps': 2, 'comparisons': 4},
   {'name': 'swap', 'state': (2, 3, 1, 4), 'indices': (3, 2), 'swaps': 3, 'comparisons': 4},
   {'name': 'compare', 'state': (2, 3, 1, 4), 'indices': (2, 1), 'swaps': 3, 'comparisons': 5},
   {'name': 'swap', 'state': (2, 1, 3, 4), 'indices': (2, 1), 'swaps': 4, 'comparisons': 5},
   {'name': 'compare', 'state': (2, 1, 3, 4), 'indices': (1, 0), 'swaps': 4, 'comparisons': 6},
   {'name': 'swap', 'state': (1, 2, 3, 4), 'indices': (1, 0), 'swaps': 5, 'comparisons': 6},
   {'name': 'final', 'state': (1, 2, 3, 4), 'indices': (None, None), 'swaps': 5, 'comparisons': 6}
]

view

In order to visualize a step-by-step of the algorithm, you can pass the events generated by the engine into a view and animate it!

  from engine import Engine
  from view import View

  engine = Engine()
  engine.insertion_sort([5, 6, 8, 7, 10, 4, 9, 3, 2, 1])

  view = View("insertion sort", engine.events, width=6, height=4)
  view.animate(delay=200, save=True)

insertion sort animation example

algorithms

Currently, the algorithms available are:

  • bubble sort
  • selection sort
  • insertion sort
  • merge sort
  • quick sort

tests

Running tests:

  $ python3 -m pytest test.py

About

Engine to study and visualize algorithms

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0