c# 排序算法可视化

最近在 b 站上看了一个排序算法的动画,所以想自己写一个类似的项目。

项目使用 Graphics 在 winform 的窗体上绘图。水平精力有限,只尝试了直接插入的算法。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sort
{
    /// <summary>
    /// 排序算法
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            FrmSort frm = new FrmSort();
            frm.ShowDialog();
        }
    }

    class FrmSort : Form
    {
        private const int length = 200; //数组宽度
        private const int width = 2;   //绘图时每个柱子的宽度
        private const int height = 2;   //绘图时每个柱子的高度放大倍数

        public FrmSort()
        {
            this.BackColor = Color.White;
            this.Width = 600;
            this.Height = 600;
            this.Shown += new System.EventHandler(this.Frm_Shown);
        }

        private void Frm_Shown(object sender, EventArgs e)
        {
            Insert();
        }

        //获得随机数组
        static List<int> GetRandomList()
        {
            Random rnd = new Random(1);
            List<int> listOriginal = new List<int>();
            for (int i = 1; i < (length + 1); i++)
            {
                listOriginal.Add(i);
            }
            List<int> list = new List<int>();
            foreach (int item in listOriginal)
            {
                list.Insert(rnd.Next(list.Count), item);
            }
            return list;
        }

        //绘图
        private void Draw(List<int> list, int start, int end, Graphics g, int xBase, int yBase)
        {
            {
                Pen whitePen = new Pen(Brushes.White, (end - start) * width);
                Point point1 = new Point(xBase + start * width, yBase);
                Point point2 = new Point(xBase + start * width, yBase + list.Count * height);
                g.DrawLine(whitePen, point1, point2);
            }

            Pen blackPen = new Pen(Brushes.Black, width);
            for (int i = start; i <= end; i++)
            {
                Point point1 = new Point(xBase + i * width, yBase);
                Point point2 = new Point(xBase + i * width, yBase + list[i] * height);
                g.DrawLine(blackPen, point1, point2);
            }
        }

        //直接插入
        private void Insert()
        {
            int xBase = 20;
            int yBase = 20;
            List<int> list = GetRandomList();
            using (Graphics g = this.CreateGraphics())
            {
                Draw(list, 0, list.Count - 1, g, xBase, yBase);
                for (int i = 1; i < list.Count; i++)
                {
                    for (int j = i - 1; (j >= 0) && (list[j + 1] < list[j]); j--)
                    {
                        int temp = list[j + 1];
                        list[j + 1] = list[j];
                        list[j] = temp;
                        Draw(list, j, j + 1, g, xBase, yBase);
                    }
                }
                Draw(list, 0, list.Count - 1, g, xBase, yBase);
            }
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/aitong/p/10959613.html
今日推荐