当前位置: 首页 > news >正文

C#,冒泡排序算法(Bubble Sort)的源代码与数据可视化

排序算法是编程的基础。

 常见的四种排序算法是:简单选择排序、冒泡排序、插入排序和快速排序。其中的快速排序的优势明显,一般使用递归方式实现,但遇到数据量大的情况则无法适用。实际工程中一般使用“非递归”方式实现。本文搜集发布四种算法的源代码及非递归快速排序的代码。

冒泡排序(Bubble Sort)算法

思路:从左到右,将相邻的进行比较,若前面数值大于后面数值,则交换,否则不交换。

代码改编自:C#实现常见排序算法_菜园赤子的博客-CSDN博客_c#排序算法

代码:

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        Random rnd = new Random((int)DateTime.Now.Ticks);
        List<string> slides = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "C#,四种常见排序算法的可视化编程——北京联高软件开发有限公司";
            button1.Text = "选择排序"; button1.Cursor = Cursors.Hand;
            button2.Text = "冒泡排序"; button2.Cursor = Cursors.Hand;
            button3.Text = "插入排序"; button3.Cursor = Cursors.Hand;
            button4.Text = "快速(递归)"; button4.Cursor = Cursors.Hand;
            button5.Text = "快速(非递归)"; button5.Cursor = Cursors.Hand;
            panel1.Dock = DockStyle.Top;
            panel2.Dock = DockStyle.Fill;
            webBrowser1.Navigate("http://www.315soft.com");
        }

        private int[] RandArray()
        {
            int n = 20;
            int[] dataArray = new int[n];
            for (int i = 0; i < n; i++)
            {
                dataArray[i] = rnd.Next(20, 100);
            }
            return dataArray;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            slides.Clear();
            BubbleSort(RandArray());
            loop = 0;
            timer1.Interval = 100;
            timer1.Enabled = true;
        }

        /// <summary>
        /// 冒泡排序
        /// 改编自:https://blog.csdn.net/qq_36238093/article/details/97051032
        /// </summary>
        /// <param name="dataArray"></param>
        public void BubbleSort(ref int[] dataArray)
        {
            for (int i = 0; i < dataArray.Length; i++)
            {
                bool flag = true;
                for (int j = 0; j < dataArray.Length - i - 1; j++)
                {
                    if (dataArray[j] > dataArray[j + 1])
                    {
                        int temp = dataArray[j];
                        dataArray[j] = dataArray[j + 1];
                        dataArray[j + 1] = temp;
                        flag = false;
                        slides.Add(Slide(button2.Text, dataArray, i, j));
                    }
                }
                if (flag)
                {
                    break;
                }
            }
        }

        private string Slide(string title, int[] dataArray, int a, int b)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            sb.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" >");
            sb.AppendLine("<head>");
            sb.AppendLine("<style>");
            sb.AppendLine("td { vertical-align:bottom;text-align:center;font-size:12px; } ");
            sb.AppendLine(".bar { width:" + (int)((webBrowser1.Width - dataArray.Length * 11) / dataArray.Length) + "px;font-size:12px;border:solid 1px #FF6701;background-color:#F08080;text-align:center;border-radius:3px; }");
            sb.AppendLine("</style>");
            sb.AppendLine("</head>");
            sb.AppendLine("<body>");
            sb.AppendLine("<table width='100%' style='border-bottom:solid 1px #E9E9E0;'>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td>方法:" + title + "</td>");
            sb.AppendLine("<td>数据:" + dataArray.Length + "</td>");
            sb.AppendLine("<td>步骤:[0]</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("</table>");
            sb.AppendLine("<br>");
            sb.AppendLine("<table width='100%' style='border-bottom:solid 15px #E9E9E0;'>");
            sb.AppendLine("<tr>");
            for (int i = 0; i < dataArray.Length; i++)
            {
                if (i == a || i == b)
                {
                    sb.AppendLine("<td>" + dataArray[i] + "<div class='bar' style='height:" + dataArray[i]*3 + "px;background-color:#993333;'></div></td>");
                }
                else
                {
                    sb.AppendLine("<td>" + dataArray[i] + "<div class='bar' style='height:" + dataArray[i]*3 + "px;'></div></td>");
                }
            }

            sb.AppendLine("</tr>");
            sb.AppendLine("</table>");
            sb.AppendLine("</body>");
            sb.AppendLine("</html>");
            return sb.ToString();
        }


        int loop = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (loop < slides.Count + (3000 / timer1.Interval))
            {
                if (loop < slides.Count)
                {
                    webBrowser1.DocumentText = slides[loop].Replace("[0]", loop + " / " + slides.Count);
                    loop++;
                    return;
                }
                loop++;
                return;
            }
            loop = 0;
        }

    }
}
 

相关文章:

  • CoTracker 环境配置与ORB 特征点提取结合实现视频特征点追踪
  • Grouping Increases
  • alibabacloud学习笔记02(小滴课堂)
  • [python]使用pyqt5搭建yolov8 竹签计数一次性筷子计数系统
  • 镜头选型和计算
  • Linux之Ubuntu环境Jenkins部署前端项目
  • 【uniapp】APP打包上架应用商-注意事项
  • 2024年度 ROTS - 实时操作系统 Top 15
  • 【管理篇 / 登录】❀ 06. macOS下使用USB配置线登录 ❀ FortiGate 防火墙
  • 【Nodejs】基于node http模块的博客demo代码实现
  • Go后端开发 -- Go Modules
  • x-cmd pkg | trafilatura - 网络爬虫和搜索引擎优化工具
  • async和await关键字
  • 解析千兆多模光模块SFP-GE-SX
  • 可视化速通知识点
  • 11111111
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • canvas 五子棋游戏
  • js对象的深浅拷贝
  • Mybatis初体验
  • ng6--错误信息小结(持续更新)
  • Sass Day-01
  • WePY 在小程序性能调优上做出的探究
  • 阿里云前端周刊 - 第 26 期
  • 给初学者:JavaScript 中数组操作注意点
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 前言-如何学习区块链
  • 我是如何设计 Upload 上传组件的
  • 线上 python http server profile 实践
  • 一个SAP顾问在美国的这些年
  • 原生Ajax
  • 阿里云移动端播放器高级功能介绍
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • (3)llvm ir转换过程
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (八)c52学习之旅-中断实验
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (轉)JSON.stringify 语法实例讲解
  • .apk文件,IIS不支持下载解决
  • .NET 4.0中的泛型协变和反变
  • .NET Core中的去虚
  • .NetCore项目nginx发布
  • .Net程序帮助文档制作
  • .net对接阿里云CSB服务
  • .NET开发人员必知的八个网站
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [ArcPy百科]第三节: Geometry信息中的空间参考解析
  • [Everyday Mathematics]20150130
  • [flask]http请求//获取请求头信息+客户端信息