C#, source code and data visualization of Simple Select 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.

Simple Select Sort algorithm


Algorithm idea: From left to right, use the first element as the base number to compare with the following numbers, and exchange if a smaller number is found. And so on. until the cycle ends.
Code adapted from: C# implements common sorting algorithms_Caiyuan Chizi’s blog-CSDN blog_c# sorting algorithm

Code:

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 button1_Click(object sender, EventArgs e)
        {
            slides.Clear();
            SelectionSort(RandArray());
            loop = 0;
            timer1.Interval = 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];
                        dataArray[i] = dataArray[j];
                        dataArray[j] = temp;
                        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("<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;
        }

    }
}
 

The source codes of five sorting algorithms can be downloaded:

Source code of five sorting algorithms icon-default.png?t=N7T8https://download.csdn.net/download/beijinghorn/85076171

Guess you like

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