C#,简单选择排序算法(Simple Select Sort)的源代码与数据可视化

排序算法是编程的基础。

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

简单选择排序(Simple Select 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 button1_Click(object sender, EventArgs e)

{

slides.Clear();

SelectionSort(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 SelectionSort(int[] dataArray)

{

for (int i = 0; i < dataArray.Length - 1; i++)

{

for (int j = i + 1; j < dataArray.Length; j++)

{

if (dataArray[i] > dataArray[j])

{

int temp = dataArray[i];

dataArray[i] = dataArray[j];

dataArray[j] = temp;

slides.Add(Slide(button1.Text, dataArray, i, j));

}

}

}

}

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;

}

}

}

可下载五种排序算法源代码:

五种排序算法源代码https://download.csdn.net/download/beijinghorn/85076171

相关推荐
开心-开心急了7 分钟前
关于Flutter与Qt for python 的一些技术、开源、商用等问题
开发语言·python·qt·flutter
友友马9 分钟前
『 QT 』按钮类控件属性解析
开发语言·数据库·qt
Evand J11 分钟前
【MATLAB例程】基于噪声协方差自适应的互补滤波器方法vs标准互补滤波,用于融合加速度计和陀螺仪数据,估计角度
开发语言·matlab
熊小猿18 分钟前
RabbitMQ死信交换机与延迟队列:原理、实现与最佳实践
开发语言·后端·ruby
星释30 分钟前
Rust 练习册 :Pig Latin与语言游戏
游戏·rust·c#
@小码农32 分钟前
2025年北京海淀区中小学生信息学竞赛第一赛段试题(附答案)
人工智能·python·算法·蓝桥杯
2301_7951672032 分钟前
玩转Rust高级应用 如何让让运算符支持自定义类型,通过运算符重载的方式是针对自定义类型吗?
开发语言·后端·算法·安全·rust
laocooon52385788636 分钟前
C语言 有关指针,都要学哪些内容
c语言·数据结构·算法
梦想平凡1 小时前
情怀源代码工程实践(加长版 1/3):确定性内核、事件回放与最小可运行骨架
开发语言·javascript·ecmascript