排序算法是編程的基礎(chǔ)卦洽。
?常見的四種排序算法是:簡(jiǎn)單選擇排序、冒泡排序斜棚、插入排序和快速排序阀蒂。其中的快速排序的優(yōu)勢(shì)明顯,一般使用遞歸方式實(shí)現(xiàn)弟蚀,但遇到數(shù)據(jù)量大的情況則無法適用脂新。實(shí)際工程中一般使用“非遞歸”方式實(shí)現(xiàn)。本文搜集發(fā)布四種算法的源代碼及非遞歸快速排序的代碼粗梭。
插入排序(Insert Sort)算法
思路:從左到右争便,從第二元素開始,與它前面的數(shù)比較断医,找到小于它的數(shù)就將數(shù)值插入元素前面滞乙。
代碼改編自:C#實(shí)現(xiàn)常見排序算法_菜園赤子的博客-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#,四種常見排序算法的可視化編程——北京聯(lián)高軟件開發(fā)有限公司";
???????????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 button3_Click(object sender, EventArgs e)
???????{
???????????slides.Clear();
???????????InsertionSort(RandArray());
???????????loop = 0;
???????????timer1.Interval = 100;
???????????timer1.Enabled = true;
???????}
???????///
???????///插入排序
???????///改編自:https://blog.csdn.net/qq_36238093/article/details/97051032
???????///
???????///
???????public void InsertionSort(int[] dataArray)
???????{
???????????for (int i = 1; i < dataArray.Length; i++)
???????????{
??????????????? for (int j = i; j > 0; j--)
??????????????? {
?????????????????? ?if (dataArray[j] < dataArray[j - 1])
??????????????????? {
??????????????????????? int temp =dataArray[j];
??????????????????????? dataArray[j] =dataArray[j - 1];
??????????????????????? dataArray[j - 1] =temp;
??????????????????????? slides.Add(Slide(button3.Text,dataArray, i, j));
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? break;
??????????????????? }
??????????????? }
???????????}
???????}
???????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("數(shù)據(jù):" +
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;
???????}
??? }
}
可下載五種排序算法源代碼學(xué)習(xí)鉴嗤。