並べ替えアルゴリズムはプログラミングの基礎です。
一般的な 4 つの並べ替えアルゴリズムは、単純選択並べ替え、バブル 並べ替え、挿入並べ替え、およびクイック 並べ替えです。中でもクイックソートは明らかな利点があり、一般的には再帰を利用して実装されますが、データ量が多い場合には適用できません。実際のプロジェクトでは「非再帰的」な方法が一般的に使われます。この記事では、4 つのアルゴリズムのソース コードと非再帰的クイック ソートのコードを収集して公開します。
シンプルな選択ソートアルゴリズム
アルゴリズムのアイデア: 左から右に、最初の要素を基数として使用して次の数値と比較し、より小さい数値が見つかった場合は交換します。等々。サイクルが終わるまで。
引用元のコード: C# は一般的な並べ替えアルゴリズムを実装します_Caiyuan Chizi のブログ - CSDN ブログ_c# 並べ替えアルゴリズム
コード:
システムを使用する;
System.Text を使用します。
System.Collections.Generic を使用します。
System.Windows.Forms を使用します。
namespace WindowsFormsApp6
{ public Partial class Form1 : Form { Random rnd = new Random((int)DateTime.Now.Ticks); List<string> スライド = new List<string>();
public Form1()
{ InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{ this.Text = "C#、4 つの一般的な並べ替えアルゴリズムのビジュアル プログラミング - 北京良澳ソフトウェア開発有限公司"; 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 = 新しい int[n]; for (int i = 0; i < n; i++) { dataArray[i] = rnd.Next(20, 100); データ配列を返します 。 }
private void button1_Click(object sender, EventArgs e)
{ slides.Clear(); 選択ソート(RandArray()); ループ = 0; タイマー1.間隔 = 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]; データ配列[i] = データ配列[j]; データ配列[j] = 一時; 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("<頭>"); 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("</style>"); sb.AppendLine("</head>"); sb.AppendLine("<本文>"); sb.AppendLine("<table width='100%' style='border-bottom:solid 1px #E9E9E0;'>"); sb.AppendLine("<tr>"); sb.AppendLine("<td>方法:" + タイトル + "</td>"); sb.AppendLine("<td>データ:" + dataArray.Length + "</td>"); sb.AppendLine("<td>步骤:[0]</td>"); sb.AppendLine("</tr>"); sb.AppendLine("</テーブル>"); 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("</テーブル>");
sb.AppendLine("</body>");
sb.AppendLine("</html>");
sb.ToString() を返します。
}
int ループ = 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]", ループ + " / " + スライド.カウント); ループ++; 戻る; ループ ++; 戻る; ループ = 0; }
}
}
5 つの並べ替えアルゴリズムのソース コードをダウンロードできます。
5 つの並べ替えアルゴリズムのソースコードhttps://download.csdn.net/download/beijinghorn/85076171