✨ From vibe coding to vibe deployment. UBOS MCP turns ideas into infra with one message.

Learn more
Andrii Bidochko
  • Updated: February 14, 2026
  • 8 min read

Bubble Sort on Turing Machine: Open‑Source Project Brings Algorithmic Education to UBOS

Bubble Sort on Turing Machine illustration

Bubble Sort on a Turing Machine is an open‑source project that demonstrates how a classic sorting algorithm can be executed on a theoretical Turing Machine, providing both decimal and unary variants together with a lightweight Python emulator.

1. Introduction to Bubble Sort and Turing Machines

Bubble sort is one of the most recognizable sorting algorithms in computer science. It repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. Although its O(n²) time complexity makes it inefficient for large datasets, its simplicity makes it an ideal teaching tool.

A Turing machine (TM) is a mathematical model of computation that manipulates symbols on an infinite tape according to a set of rules. By implementing bubble sort on a TM, developers can explore how high‑level algorithms translate into low‑level state transitions, deepening their understanding of both algorithmic logic and the fundamentals of computation.

For software developers and computer‑science students, visualizing this process bridges the gap between abstract theory and practical coding. The project we discuss below does exactly that, and it does so in a way that is instantly runnable in a browser‑based emulator or locally via Python.

2. Overview of the GitHub Project

The Bubble Sort on Turing Machine repository is hosted on GitHub under an MIT license, making it free for personal, educational, and commercial use. The repo contains:

  • bubble_sort.yaml – a decimal‑encoded TM (each digit occupies a single tape cell).
  • bubble_sort_unary.yaml – a unary‑encoded TM (numbers are represented by sequences of 1 separated by 0 delimiters).
  • emulator.py – a pure‑Python TM emulator that reads the YAML files and executes them step‑by‑step.
  • Test suite, Makefile, and a generator script that can produce a custom decimal TM for any digit string.

The project follows a clean, modular layout that mirrors best practices for open‑source algorithm visualizations. All files are ready to be pasted into turingmachine.io for an interactive, browser‑based view of the sorting process.

3. Features of the Decimal and Unary TM Variants

3.1 Decimal Variant (bubble_sort.yaml)

In the decimal version each tape cell holds a single digit from 1 to 7. The machine executes a classic bubble‑sort pass by:

  1. Reading a pair of adjacent digits.
  2. Remembering the left digit in the current state.
  3. Comparing it with the right digit and swapping if necessary.
  4. Repeating the pass until a full sweep makes no swaps, at which point the TM halts.

The implementation uses 25 states grouped into “clean” and “dirty” passes, a technique that tracks whether any swap occurred during the current sweep. For the example input 327154, the TM finishes in 63 steps, producing the sorted output 123457.

3.2 Unary Variant (bubble_sort_unary.yaml)

Unary encoding represents a number n as n consecutive 1 symbols, with 0 acting as a delimiter between numbers. This representation forces the TM to perform a more sophisticated comparison:

  • Mark one unit in the right operand (B) and one in the left operand (A) alternately.
  • If the right side runs out first, the left number is larger and a swap is required.
  • Swapping moves the “excess” X marks across the delimiter, effectively bubbling the larger value rightward.

The unary TM comprises 31 states** organized into six functional groups (delimiter seeking, comparison, excess checking, swapping, restoration, and rewind). For the input [3,2,7,1,5,4] (encoded as 111011011111110101111101111) the machine halts after 1,424 steps, yielding the sorted sequence [1,2,3,4,5,7].

Both variants are fully compatible with the turingmachine.io YAML format, allowing you to watch each transition in real time. The visual feedback—especially in the decimal version where larger digits visibly “bubble” to the right—makes the abstract algorithm concrete.

4. How to Use the Emulator

The emulator.py script is a lightweight, dependency‑free Python program (requires only PyYAML) that can run any TM defined in the supported YAML schema. Below is a quick‑start guide:

# Install the only required package
pip install pyyaml

# Run the decimal TM
python3 emulator.py bubble_sort.yaml

# Run the unary TM
python3 emulator.py bubble_sort_unary.yaml -v   # verbose mode shows each step

The emulator offers several useful flags:

  • -v – prints every transition, ideal for classroom demos.
  • -n <steps> – limits execution to a maximum number of steps, preventing infinite loops.
  • --json – outputs the final tape state as JSON for downstream processing.

For developers who prefer a programmatic interface, the script can be imported as a module:

from emulator import TuringMachine
import yaml

with open("bubble_sort.yaml") as f:
    cfg = yaml.safe_load(f.read())
tm = TuringMachine(cfg)

while not tm.halted():
    tm.step()

print("Result:", tm.tape_contents())

The test suite (invoked via make test) runs 135 unit tests covering every permutation of small inputs, ensuring the TM behaves correctly across edge cases. This rigorous testing framework makes the project a reliable teaching resource.

5. Benefits for Learning and Development

Integrating this project into a curriculum or a personal study plan yields several concrete advantages:

  • Deep algorithmic insight: Students see how a high‑level sorting routine decomposes into elementary tape moves and state changes.
  • State‑machine thinking: Designing or modifying the TM reinforces finite‑state automata concepts, a cornerstone of compiler construction.
  • Hands‑on debugging: The verbose emulator output acts like a low‑level debugger, letting learners step through each transition.
  • Open‑source collaboration: Forking the repo on GitHub encourages contributions, issue tracking, and version control practice.
  • Cross‑platform experimentation: Because the TM description is pure YAML, it can be loaded into any compatible visualizer, from web‑based tools to custom IDE plugins.

Moreover, the project’s architecture mirrors modern low‑code platforms. For instance, UBOS platform overview showcases a visual builder where developers assemble AI‑driven workflows without writing code. The same mindset—defining behavior through declarative configurations—applies to both Turing‑machine YAML and UBOS’s low‑code templates.

Educators can also pair the bubble‑sort TM with other AI‑centric tools from UBOS to create richer learning experiences. Imagine a lesson where students first watch the TM sorting, then use the AI marketing agents template to generate a blog post summarizing the algorithm, or employ the UBOS templates for quick start to prototype a web interface that visualizes the TM in real time.

6. Conclusion & Next Steps

The Bubble Sort on a Turing Machine project is more than a novelty; it is a practical, open‑source laboratory for anyone who wants to explore the intersection of classic algorithms and theoretical computation. By providing both decimal and unary implementations, a ready‑made Python emulator, and a comprehensive test suite, the repository lowers the barrier to entry for students, educators, and hobbyists alike.

Ready to dive in? Clone the repo, run the emulator, and start tweaking the state table. If you’re looking for a platform to turn your experiments into shareable web apps, check out the UBOS homepage and explore its About UBOS page to learn how the company supports AI‑first development.

For startups seeking rapid prototyping, the UBOS for startups program offers credits and mentorship. Small‑business teams can benefit from UBOS solutions for SMBs, while large enterprises may explore the Enterprise AI platform by UBOS.

Want to extend the bubble‑sort TM with voice feedback? UBOS’s ElevenLabs AI voice integration can read each step aloud. Need to store intermediate results in a vector database? The Chroma DB integration makes that trivial. And if you love chat‑based interfaces, pair the TM with the ChatGPT and Telegram integration to receive live sorting updates on your phone.

Finally, explore some of UBOS’s AI‑powered templates that complement algorithmic learning:

“Seeing a classic algorithm run on a Turing Machine turns abstract theory into a tangible experience—perfect for anyone serious about mastering computer science fundamentals.” – Senior Software Engineer, Open‑Source Community

Whether you are a student polishing your understanding of automata, a developer building educational tools, or an AI enthusiast looking for a fresh project, the Bubble Sort TM offers a solid foundation. Clone, experiment, and share your variations—each contribution helps the community push the boundaries of algorithmic visualization.

Get the Code on GitHub →

For more AI‑driven development resources, visit the UBOS partner program or explore the UBOS portfolio examples to see real‑world applications.


Andrii Bidochko

CTO UBOS

Andrii Bidochko is an AI entrepreneur and researcher focused on AI agents, reinforcement learning, and autonomous systems. He writes about the technologies shaping the future of machine intelligence, from frontier models and agent architectures to real-world AI applications.

Sign up for our newsletter

Stay up to date with the roadmap progress, announcements and exclusive discounts feel free to sign up with your email.

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.