国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C#,歸并排序算法(Merge Sort Algorithm)的源代碼及數(shù)據(jù)可視化

這篇具有很好參考價值的文章主要介紹了C#,歸并排序算法(Merge Sort Algorithm)的源代碼及數(shù)據(jù)可視化。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

歸并排序

歸并算法采用非常經(jīng)典的分治策略,每次把序列分成n/2的長度,將問題分解成小問題,由復(fù)雜變簡單。

因為使用了遞歸算法,不能用于大數(shù)據(jù)的排序。

c#歸并排序 代碼,C#算法演義 Algorithm Recipes,c#,開發(fā)語言,算法

核心代碼:

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();
? ? ? ? ? ? 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);
? ? ? ? ? ? }
? ? ? ? ? ? return dataArray;
? ? ? ? }

? ? ? ? private void button6_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;
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 歸并排序算法
? ? ? ? /// </summary>
? ? ? ? /// <param name="left"></param>
? ? ? ? /// <param name="right"></param>
? ? ? ? /// <param name="arraySource"></param>
? ? ? ? /// <param name="arrayTemplate"></param>
? ? ? ? private void MergeSort(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 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>數(shù)據(jù):" + 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>" + 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("</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 = slides[loop].Replace("[0]", loop + " / " + slides.Count);
? ? ? ? ? ? ? ? ? ? loop++;
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? loop++;
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? loop = 0;
? ? ? ? }
? ? }
}
?

?——————————————————————

POWER BY 315SOFT.COM &
TRUFFER.CN文章來源地址http://www.zghlxwxcb.cn/news/detail-782086.html

到了這里,關(guān)于C#,歸并排序算法(Merge Sort Algorithm)的源代碼及數(shù)據(jù)可視化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • C#,字符串匹配(模式搜索)原生(Native)算法的源代碼

    C#,字符串匹配(模式搜索)原生(Native)算法的源代碼

    算法沒什么可說的,就是一段一段匹配唄。 運行效果: ?源代碼: using System; using System.Collections; using System.Collections.Generic; namespace Legalsoft.Truffer.Algorithm { ?? ?/// summary ?? ?/// 字符串匹配(模式搜索)算法集錦 ?? ?/// /summary ?? ?public static partial class PatternSearch ?? ?{ ??

    2024年02月01日
    瀏覽(43)
  • C#,字符串匹配(模式搜索)RK(Rabin Karp)算法的源代碼

    C#,字符串匹配(模式搜索)RK(Rabin Karp)算法的源代碼

    ?M.O.Rabin Rabin-Karp算法,是由M.O.Rabin和R.A.Karp設(shè)計實現(xiàn)的一種基于移動散列值的字符串匹配算法。 通?;谏⒘兄档淖址ヅ浞椒ǎ海?)首先計算模式字符串的散列函數(shù);(2)然后利用相同的散列函數(shù)計算文本中所有可能的M個字符的子字符串的散列函數(shù)值并尋找匹配。但是

    2024年01月19日
    瀏覽(29)
  • C#,字符串匹配(模式搜索)KMP算法的源代碼與數(shù)據(jù)可視化

    C#,字符串匹配(模式搜索)KMP算法的源代碼與數(shù)據(jù)可視化

    ? D.E.Knuth ? J.H.Morris KMP 算法(Knuth-Morris-Pratt 算法)是其中一個著名的、傳統(tǒng)的字符串匹配算法,效率比較高。 KMP算法由 D.E.Knuth , J.H.Morris 和 V.R.Pratt 在 Brute-Force 算法的基礎(chǔ)上提出的模式匹配的改進算法。因此人們稱它為“克努特—莫里斯—普拉特算法”,簡稱KMP算法。K

    2024年01月25日
    瀏覽(37)
  • C#,數(shù)值計算,矩陣的喬萊斯基分解(Cholesky decomposition)算法與源代碼

    C#,數(shù)值計算,矩陣的喬萊斯基分解(Cholesky decomposition)算法與源代碼

    安德烈·路易斯·喬爾斯基出生于法國波爾多以北的查倫特斯海域的蒙古揚。他在波爾多參加了Lycée e,并于1892年11月14日獲得學(xué)士學(xué)位的第一部分,于1893年7月24日獲得第二部分。1895年10月15日,喬爾斯基進入萊科爾理工學(xué)院,在當年223名入學(xué)學(xué)生中排名第88位。他在萊科爾理工

    2024年02月22日
    瀏覽(32)
  • C#,二進制數(shù)的按位旋轉(zhuǎn)(Bits Rotate)算法與源代碼

    C#,二進制數(shù)的按位旋轉(zhuǎn)(Bits Rotate)算法與源代碼

    二進制數(shù)的按位旋轉(zhuǎn)(翻轉(zhuǎn))是編程中常見的按位運算方法。 二進制數(shù)的按位旋轉(zhuǎn)分為左轉(zhuǎn)、右轉(zhuǎn)。 左轉(zhuǎn)意味著數(shù)據(jù)變大,右轉(zhuǎn)意味著數(shù)據(jù)變?。ㄓ袚p)。 using System; using System.Text; using System.Collections; using System.Collections.Generic; namespace Legalsoft.Truffer.Algorithm { ?? ?public static

    2024年02月19日
    瀏覽(25)
  • C#,二進制數(shù)的非0位數(shù)統(tǒng)計(Bits Count)的算法與源代碼

    C#,二進制數(shù)的非0位數(shù)統(tǒng)計(Bits Count)的算法與源代碼

    計算一個十進制數(shù)的二進制表示有多少位1? 使用循環(huán)按位統(tǒng)計1的個數(shù)。 利用一個數(shù)組或哈希生成一張表,存儲不同二進制編碼對應(yīng)的值為1的二進制位數(shù),那么在使用時,只需要去進行查詢,即可在O(1)的時間復(fù)雜度內(nèi)得到結(jié)果。 但是,此算法有個弊端,由于算法是采用空間

    2024年02月22日
    瀏覽(26)
  • C#,圖論與圖算法,圖(Graph)廣度優(yōu)先遍歷(BFS,Breadth First Search)算法與源代碼

    C#,圖論與圖算法,圖(Graph)廣度優(yōu)先遍歷(BFS,Breadth First Search)算法與源代碼

    深度優(yōu)先算法(DFS,Deep First Search)與 寬度優(yōu)先遍歷(BFS,Breadth First Search) 是樹、圖數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ)性、標準性的遍歷算法。 深度優(yōu)先搜索(DFS)是一種用于搜索圖形或樹數(shù)據(jù)結(jié)構(gòu)的算法。該算法從樹的根(頂部)節(jié)點開始,盡可能沿著給定的分支(路徑)向下,然后回溯

    2024年03月23日
    瀏覽(30)
  • C#,計算幾何,隨機點集之三角剖分的德勞內(nèi)(Delaunay)算法的源代碼

    C#,計算幾何,隨機點集之三角剖分的德勞內(nèi)(Delaunay)算法的源代碼

    點集的三角剖分(Triangulation),對數(shù)值分析(比如有限元分析)以及圖形學(xué)來說,都是極為重要的一項預(yù)處理技術(shù)。尤其是Delaunay三角剖分,由于其獨特性,關(guān)于點集的很多種幾何圖都和Delaunay三角剖分相關(guān),如 Voronoi 圖, EMST 樹, Gabriel 圖等。Delaunay三角剖分有最大化最小角

    2024年03月12日
    瀏覽(32)
  • C#,數(shù)值計算,矩陣的行列式(Determinant)、伴隨矩陣(Adjoint)與逆矩陣(Inverse)的算法與源代碼

    C#,數(shù)值計算,矩陣的行列式(Determinant)、伴隨矩陣(Adjoint)與逆矩陣(Inverse)的算法與源代碼

    本文發(fā)布矩陣(Matrix)的一些初級算法。 矩陣行列式是指矩陣的全部元素構(gòu)成的行列式,設(shè)A=(a)是數(shù)域P上的一個n階矩陣,則所有A=(a)中的元素組成的行列式稱為矩陣A的行列式,記為|A|或det(A)。若A,B是數(shù)域P上的兩個n階矩陣,k是P中的任一個數(shù),則|AB|=|A||B|,|kA|=k?|A|,|A*|=

    2024年02月20日
    瀏覽(26)
  • C# 關(guān)于源代碼生成

    步驟1: 首先建立一個控制臺程序?SourceGeneratorDome1 選擇版本.net7 代碼如下: 建立類文 件??GreetingUsePartialClassm 這是一個類分布文件。 看清楚喲。這里只是定義了一個分布類和分布方法。具體實現(xiàn)方法通過源代碼生成 步驟2:建立一個源代碼生成項目 但是類型選擇.?netstanda

    2024年02月11日
    瀏覽(95)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包