C#,快速排序算法(Quick Sort)的非递归实现与数据可视化

排序算法是编程的基础。

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

快速排序(Quick Sort)算法(非递归方式)

实际工程中一般使用"非递归"方式实现。

代码改编自:

做良心程序员,工程文件与源代码:

下载吧https://download.csdn.net/download/beijinghorn/85076171

代码:

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 intn;

for (int i = 0; i < n; i++)

{

dataArrayi = rnd.Next(20, 100);

}

return dataArray;

}

private void button5_Click(object sender, EventArgs e)

{

QuickSort(RandArray());

loop = 0;

timer1.Interval = 100;

timer1.Enabled = true;

}

/// <summary>

/// 快速排序(非递归)

/// 改编自:https://blog.csdn.net/syaguang2006/article/details/24441025

/// </summary>

/// <param name="dataArray"></param>

/// <param name="Low"></param>

/// <param name="High"></param>

/// <param name="stack"></param>

private void QuickSort(ref int\[\] dataArray, int Low, int High, Stack<int> stack)

{

int low = Low;

int high = High;

int temp = dataArraylow;

while (high > low)

{

while (low < high && temp <= dataArrayhigh)

{

high--;

}

if (high > low)

{

dataArraylow = dataArrayhigh;

dataArrayhigh = temp;

}

while (low < high && temp >= dataArraylow)

{

low++;

}

if (high > low)

{

dataArrayhigh = dataArraylow;

dataArraylow = temp;

}

if (low == high)

{

if (Low < low - 1)

{

stack.Push(Low);

stack.Push(low - 1);

}

if (High > low + 1)

{

stack.Push(low + 1);

stack.Push(High);

}

}

}

}

private void QuickSort(ref int\[\] dataArray)

{

Stack<int> s = new Stack<int>();

s.Push(0);

s.Push(dataArray.Length - 1);

while (s.Count > 0)

{

int low = s.Pop();

int high = s.Pop();

if (low > high)

{

int temp = low;

low = high;

high = temp;

}

QuickSort(ref dataArray, low, high, s);

slides.Add(Slide(button5.Text, dataArray, low, high));

}

}

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>" + dataArrayi + "<div class='bar' style='height:" + dataArrayi*3 + "px;background-color:#993333;'></div></td>");

}

else

{

sb.AppendLine("<td>" + dataArrayi + "<div class='bar' style='height:" + dataArrayi*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 = slidesloop.Replace("0", loop + " / " + slides.Count);

loop++;

return;

}

loop++;

return;

}

loop = 0;

}

}

}

相关推荐
frjc4 分钟前
阿里云 ACK 环境 Arthas 在线调试指南
开发语言·python
happyprince35 分钟前
03-深刻观-CodeX哲学与升华(源码)
算法·ai编程
不会打球的王子38 分钟前
Day 27:迁移学习与微调 — 站在巨人的肩膀上
算法
longxiaozhang61 小时前
C#继承:让代码少写一点,偷懒的好方法
开发语言·c#
weixin_440730501 小时前
python+request实现接口-参数化小结
开发语言·python
CHHH_HHH1 小时前
【Linux系统篇】深入解析Linux文件系统:从磁盘寻址到软硬链接
linux·服务器·开发语言·后端·ubuntu
yngsqq1 小时前
窗体快速取消
java·开发语言
数模竞赛Paid answer1 小时前
2025年中青杯数学建模A题康养城市建设求解全过程论文及程序
算法·数学建模·数据分析·中青杯
网安蟹佬霸1 小时前
密码学安全实战:从加密原理到哈希破解的完整攻防指南
网络·算法·安全·web安全·开源·密码学·哈希算法
rockey6272 小时前
C#脚本引擎之AScript与DynamicExpresso对比
c#·.net·script·动态脚本