歸并算法采用非常經(jīng)典的分治策略蒿赢,每次把序列分成n/2的長(zhǎng)度,將問題分解成小問題,由復(fù)雜變簡(jiǎn)單鸭轮。
因?yàn)槭褂昧?a target="_blank">遞歸算法,不能用于大數(shù)據(jù)的排序橄霉。
核心代碼:
using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApp6
{
??? public partial classForm1 : Form
??? {
??????? Random rnd = newRandom((int)DateTime.Now.Ticks);
??????? Listslides = new List();
??????? public Form1()
??????? {
???????????InitializeComponent();
???????????BrowserReleaseHelper.SetWebBrowserFeatures(11);
??????? }
????? ??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;
??????????? button6.Text ="歸并排序"; button6.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);
??????????? }
??????????? returndataArray;
??????? }
??????? private voidbutton6_Click(object sender, EventArgs e)
??????? {
??????????? int[]arraySource = RandArray();
??????????? int[]arrayTemplate = new int[arraySource.Length];
??????????? MergeSort(0,arraySource.Length - 1, ref arraySource, ref arrayTemplate);
??????????? loop = 0;
???????????timer1.Interval = 100;
?? ?????????timer1.Enabled = true;
??????? }
??????? ///
??????? ///歸并排序算法
??????? ///
??????? ///
??????? ///
??????? ///
??????? ///
??????? private voidMergeSort(int left, int right, ref int[] arraySource, ref int[] arrayTemplate)
??????? {
??????????? if (left >=right)
??????????? {
??????????????? return;
??????????? }
??????????? int mid =(left + right) >> 1;
???????????MergeSort(left, mid, ref arraySource, ref arrayTemplate);
??????????? MergeSort(mid+ 1, right, ref arraySource, ref arrayTemplate);
??????????? int head_left= left;
??????????? int head_right= mid + 1;
?????????? ?int tmp_index = left;
??????????? while(head_left <= mid && head_right <= right)
??????????? {
??????????????? if(arraySource[head_left] < arraySource[head_right])
??????????????? {
???????????????????arrayTemplate[tmp_index++] = arraySource[head_left++];
??????????????? }
??????????????? else
??????????????? {
???????????????????arrayTemplate[tmp_index++] = arraySource[head_right++];
??????????????? }
??????????? }
??????????? while(head_left <= mid)
??????????? {
???????????????arrayTemplate[tmp_index++] = arraySource[head_left++];
??????????? }
??????????? while(head_right <= right)
??????????? {
???????????????arrayTemplate[tmp_index++] = arraySource[head_right++];
??????????? }
??????????? for (int i =left; i <= right; i++)
??????????? {
???????????????arraySource[i] = arrayTemplate[i];
??????????? }
???????????slides.Add(Slide(button6.Text, arraySource, left, right));
??????? }
??????? private stringSlide(string title, int[] dataArray, int a, int b)
??????? {
??????????? StringBuildersb = 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 + "");
???????????sb.AppendLine("數(shù)據(jù):" +dataArray.Length + "");
??????????? sb.AppendLine("步驟:[0]");
???????????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("");
??????????? returnsb.ToString();
??????? }
??????? int loop = 0;
??????? private void timer1_Tick(object sender,EventArgs e)
??????? {
??????????? if (loop
??????????? {
??????????????? if (loop< slides.Count)
??????????????? {
???????????????????webBrowser1.DocumentText = slides[loop].Replace("[0]", loop +" / " + slides.Count);
???????????????????loop++;
???????????????????return;
??????????????? }
??????????????? loop++;
??????????????? return;
??????????? }
??????????? loop = 0;
??????? }
??? }
}