排序算法是编程的基础。
常见的四种排序算法是:简单选择排序、冒泡排序、插入排序和快速排序。其中的快速排序的优势明显,一般使用递归方式实现,但遇到数据量大的情况则无法适用。实际工程中一般使用“非递归”方式实现。本文搜集发布四种算法的源代码及非递归快速排序的代码。
快速排序(Quick Sort)算法(递归方式)
快速排序的思想是分治法 (Divide-and-ConquerMethod)。先从数列中取出一个数作为基准数。分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。再对左右区间重复第二步,直到各区间只有一个数。
代码改编自: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 slides = new List();
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 button4_Click(object sender, EventArgs e)
{
int[] dataArray = RandArray();
slides.Clear();
QuickSort(dataArray, 0, dataArray.Length - 1);
loop = 0;
timer1.Interval = 100;
timer1.Enabled = true;
}
///
///快速排序(递归)
///改编自:https://blog.csdn.net/qq_36238093/article/details/97051032
///源代码有明显的 BUG
///
///
///
///
public void QuickSort(int[] dataArray, int left, int right)
{
int i = left;
int j = right;
int temp = dataArray[left];
if (left >= right)
{
return;
}
while (i != j)
{
while (i < j &&dataArray[j] >= temp)
{
j--;
}
if (j > i)
{
dataArray[i] =dataArray[j];
}
while (i < j &&dataArray[i] <= temp)
{
i++;
}
if (i < j)
{
dataArray[j] =dataArray[i];
}
slides.Add(Slide(button4.Text,dataArray, i, j));
}
dataArray[i] = temp;
//下面两句源代码有错误!
if (i > 0) QuickSort(dataArray, left, i - 1);
if ((i + 1) < dataArray.Length) QuickSort(dataArray, i + 1, right);
}
private string Slide(string title, int[] dataArray, int a, int b)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
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("");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("方法:" + title +
"</td>");
sb.AppendLine("数据:" +
dataArray.Length + "</td>");
sb.AppendLine("步骤:[0]</td>");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("
");
sb.AppendLine("");
sb.AppendLine("");
for (int i = 0; i < dataArray.Length; i++)
{
if (i == a || i == b)
{
sb.AppendLine(""+ dataArray[i] + "
");}
else
{
sb.AppendLine("" + dataArray[i] + "");
}
}
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("");
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;
}
}
}
可下载排序算法源代码练习。