Skip to main content

Visualize sorting algorithms with pure Python syntax / 用纯Python语法可视化排序算法

Project description

visortpy

Visualize sorting algorithms with pure Python syntax.

用纯 Python 语法可视化排序算法。

Author / 作者: pronoobe


Features / 特性

  • Write sorting algorithms in plain Python — no visualization code needed
  • 用原生 Python 写排序算法,无需任何可视化代码
  • Auto-records every comparison, read, write, and swap
  • 自动记录每次比较、读取、写入和交换操作
  • Generates GIF / MP4 animations with color-coded highlights
  • 生成带颜色高亮的 GIF / MP4 动画
  • Optional "final sweep" animation (bars turn green left-to-right)
  • 可选的"最终扫描"动画(柱状图从左到右依次变绿)
  • Modular renderer architecture — easy to extend
  • 模块化渲染器架构,易于扩展

Installation / 安装

pip install visortpy

Quick Start / 快速开始

Bubble Sort / 冒泡排序

from visortpy import VisualArray, create_video

arr = VisualArray([64, 34, 25, 12, 22, 11, 90])

for i in range(len(arr)):
    for j in range(len(arr) - i - 1):
        if arr[j] > arr[j + 1]:
            arr.swap(j, j + 1)

create_video(arr.history, output="bubble_sort.gif", fps=4)

Selection Sort / 选择排序

arr = VisualArray([29, 10, 14, 37, 13])

for i in range(len(arr)):
    min_idx = i
    for j in range(i + 1, len(arr)):
        if arr[j] < arr[min_idx]:
            min_idx = j
    if min_idx != i:
        arr.swap(i, min_idx)

create_video(arr.history, output="selection_sort.gif", fps=4, style="dark")

Quick Sort / 快速排序

from visortpy import VisualArray, create_video

def quicksort(arr, low, high):
    if low < high:
        pivot = arr[high]
        i = low - 1
        for j in range(low, high):
            if arr[j] <= pivot:
                i += 1
                arr.swap(i, j)
        arr.swap(i + 1, high)
        quicksort(arr, low, i)
        quicksort(arr, i + 2, high)

data = VisualArray([38, 27, 43, 3, 9, 82, 10])
quicksort(data, 0, len(data) - 1)
create_video(data.history, output="quick_sort.gif", fps=6)

Final Sweep Animation / 最终扫描动画

Add final_sweep=True to show a left-to-right green sweep after sorting completes.

排序完成后,加上 final_sweep=True 可以显示从左到右逐个变绿的动画。

create_video(arr.history, output="sorted.gif", fps=8, final_sweep=True)

Configuration / 配置项

Parameter / 参数 Default / 默认值 Description / 说明
output "output.mp4" Output path (.mp4, .gif) / 输出路径
fps 4 Frames per second / 帧率
style "default" Preset style: "default", "dark", "sound" / 预设风格
bar_color "#4a90d9" Default bar color / 默认柱状图颜色
highlight_compare "#e74c3c" Compare highlight (red) / 比较高亮色
highlight_swap "#2ecc71" Swap highlight (green) / 交换高亮色
final_sweep False Enable final green sweep / 启用最终扫描动画
sweep_color "#2ecc71" Sweep bar color / 扫描动画颜色

Architecture / 架构

VisualArray  ──records──>  history (List[OperationRecord])  ──renders──>  GIF/MP4
   算法操作         记录为         操作历史序列                  渲染为       动画文件

The core design decouples algorithms from rendering. VisualArray behaves like a normal Python list — your algorithm code stays clean and testable.

核心设计将算法与渲染解耦。VisualArray 的行为与普通 Python 列表一致,算法代码保持简洁可测试。

License / 许可

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

visortpy-1.0.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

visortpy-1.0.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file visortpy-1.0.0.tar.gz.

File metadata

  • Download URL: visortpy-1.0.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for visortpy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5eb5112483db8b663409e8467421ad9658f5e1072af21781438a942a88141f97
MD5 2286f20ca74860a92b43627882857abf
BLAKE2b-256 d6229cdd1b2cabc3ec657055370c523f865c30d7228e3ae29a4975ba3a2cc5c1

See more details on using hashes here.

File details

Details for the file visortpy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: visortpy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for visortpy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb7c84aaef0e6d8135a5de27a2daa159a47384ecb58cb6d874b015f7c3e86798
MD5 287fb015ba8a1d3c2f57a4e8534184a7
BLAKE2b-256 5fb6bed9a2f53a630392a6d85cfc9bb0487abc00660e2afae88aa741c5a519b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page