miniworlds-data#
miniworlds-data provides animated data-structure visualizations built on top of miniworlds. Each world renders one data structure and exposes high-level operations so algorithm implementations stay readable; the visualization is a side-effect of calling the operations.
Installation#
pip install miniworlds-data
Color language#
All data worlds share the same color vocabulary:
State |
Color |
Meaning |
|---|---|---|
|
blue-grey |
idle / unsorted |
|
yellow |
currently being compared |
|
red |
about to be swapped |
|
green |
final position reached |
|
purple |
pivot / current key |
|
dark blue |
arbitrary marker, for example min/max index |
|
green |
already visited node |
|
yellow |
node being processed right now |
|
purple |
discovered but not yet processed |
|
red |
search origin |
|
blue |
search goal |
|
green |
path edges |
Quick start: Bubble Sort#
from miniworlds_data import ListWorld, Stepper
initial = [5, 2, 4, 1, 3]
world = ListWorld(initial)
def bubble_sort(data):
n = len(data.values)
for i in range(n):
for j in range(n - i - 1):
yield "compare", (j, j + 1)
if data.values[j] > data.values[j + 1]:
yield "swap", (j, j + 1)
data.mark_sorted(n - 1 - i)
def apply_step(step):
name, payload = step
if name == "compare":
world.compare(*payload)
elif name == "swap":
world.swap(*payload)
def on_reset():
for i, value in enumerate(initial):
world.set_value(i, value)
world.reset_all()
stepper = Stepper(bubble_sort(world), world, on_step=apply_step, on_reset=on_reset)
stepper.run()
world.run()