C#, recursive implementation and data visualization of Quick Sort algorithm

Sorting algorithms are the basis of programming.

The four common sorting algorithms are: simple selection sort, bubble sort, insertion sort and quick sort. Among them, quick sort has obvious advantages. It is generally implemented using recursion, but it cannot be applied when the amount of data is large. In actual projects, the "non-recursive" method is generally used. This article collects and publishes the source code of four algorithms and the code of non-recursive quick sorting.

1 Quick Sort algorithm (recursive method)


The idea of ​​​​quick sort is the divide-and-conquer method (Divide-and-ConquerMethod). First take a number from the sequence as the base number. During the partitioning process, all numbers larger than this number are placed on its right side, and all numbers smaller than or equal to this number are placed on its left side. Repeat the second step for the left and right intervals until there is only one number in each interval.

Code adapted from: C# implements common sorting algorithms_Caiyuan Chizi’s blog-CSDN blog_c# sorting algorithm

2 Form1.cs code

text format

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

        private void Form1_Load(object sender, EventArgs e)
        {             this.Text = "C#, visual programming of four common sorting algorithms - Beijing Liangao Software Development Co., Ltd.";             button1.Text = "Selection sort"; button1.Cursor = Cursors.Hand;             button2.Text = "Bubble Sort"; button2.Cursor = Cursors.Hand;             button3.Text = "Insertion Sort"; button3.Cursor = Cursors.Hand;             button4.Text = "Quick (Recursive)"; button4.Cursor = Cursors.Hand;             button5.Text = "Quick (non-recursive)"; 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;
        }

        /// <summary>
        /// Quick sort (recursive)
        /// Adapted from: https://blog.csdn.net/qq_36238093/article/details/97051032
        /// The source code has obvious BUG
        /// < /summary>
        /// <param name="dataArray"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        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;
            // There are errors in the source code of the following two sentences!
            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("<!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>数据:" + 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;
        }

    }
}
 

Code format

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

        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;
        }

        /// <summary>
        /// 快速排序(递归)
        /// 改编自:https://blog.csdn.net/qq_36238093/article/details/97051032
        /// 源代码有明显的 BUG
        /// </summary>
        /// <param name="dataArray"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        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("<!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>数据:" + 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;
        }

    }
}

3 Form1.Designer.cs source code

namespace WindowsFormsApp6
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button5 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button6 = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.button6);
            this.panel1.Controls.Add(this.button5);
            this.panel1.Controls.Add(this.button4);
            this.panel1.Controls.Add(this.button3);
            this.panel1.Controls.Add(this.button2);
            this.panel1.Controls.Add(this.button1);
            this.panel1.Location = new System.Drawing.Point(13, 13);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1275, 103);
            this.panel1.TabIndex = 0;
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(786, 19);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(186, 61);
            this.button5.TabIndex = 4;
            this.button5.Text = "button5";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(594, 19);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(186, 61);
            this.button4.TabIndex = 3;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(402, 19);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(186, 61);
            this.button3.TabIndex = 2;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(210, 19);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(186, 61);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(18, 19);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(186, 61);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.webBrowser1);
            this.panel2.Location = new System.Drawing.Point(13, 227);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(1084, 583);
            this.panel2.TabIndex = 1;
            // 
            // webBrowser1
            // 
            this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.webBrowser1.Location = new System.Drawing.Point(0, 0);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(1084, 583);
            this.webBrowser1.TabIndex = 0;
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(978, 19);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(186, 61);
            this.button6.TabIndex = 5;
            this.button6.Text = "button6";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1300, 849);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.WebBrowser webBrowser1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
    }
}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/123919273