C#一维数组排序方法:选择排序法

目录

一、数组元素常见的排序法

1.选择排序法

二、实例1:选择排序法

1.源码

2.生成效果 


一、数组元素常见的排序法

        常见的排序法:选择排序法、冒泡排序法、快速排序法、直接插入法、希尔排序法、Array.Sort方法。

1.选择排序法

        通过遍历实现排序,第i次遍历获得index=i以后的元素中的最小值,然后与index=i的元素互换。直至遍历结束。

二、实例1:选择排序法

1.源码

// 选择排序法
using System.Xml.Linq;

namespace _099
{
    public partial class Form1 : Form
    {
        private Label? label1;
        private Label? label2;
        private TextBox? textBox1;
        private TextBox? textBox2;
        private int[]? int_array;//定义数组字段
        private readonly Random? random = new();//创建随机数对象

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(12, 9),
                Name = "label1",
                Size = new Size(68, 17),
                TabIndex = 0,
                Text = "生成数组:"
            };
            label1.Click += Label1_Click;
            // 
            // label2
            // 
            label2 = new Label
            {
                AutoSize = true,
                Location = new Point(12, 82),
                Name = "label2",
                Size = new Size(68, 17),
                TabIndex = 1,
                Text = "数组排序:"
            };
            label2.Click += Label2_Click;
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(12, 29),
                Multiline = true,
                Name = "textBox1",
                Size = new Size(300, 44),
                TabIndex = 2
            };
            // 
            // textBox2
            // 
            textBox2 = new TextBox
            {
                Location = new Point(12, 102),
                Multiline = true,
                Name = "textBox2",
                Size = new Size(300, 44),
                TabIndex = 3
            };
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(329, 157);
            Controls.Add(textBox2);
            Controls.Add(textBox1);
            Controls.Add(label2);
            Controls.Add(label1);
            Name = "Form1";
            Text = "选择排序法";
        }
        /// 
        /// 生成随机数数组
        /// 随机生成数组长度,随机生成数组元素,遍历输出
        /// 
        private void Label1_Click(object? sender, EventArgs e)
        {
            int_array = new int[random!.Next(10, 20)];
            for (int i = 0; i < int_array.Length; i++)
            {
                int_array[i] = random.Next(0, 100);
            }
            textBox1!.Clear();
            foreach (int i in int_array)
            {
                textBox1.Text += i.ToString() + ", ";
            }
        }
        /// 
        /// 数组排序
        /// 
        private void Label2_Click(object? sender, EventArgs e)
        {
            if (int_array != null)
            {
                int min;
                for (int i = 0; i < int_array!.GetUpperBound(0); i++)
                {
                    min = i;
                    for (int j = i + 1; j < int_array.Length; j++)//循环访问数组中第i+1后面的元素
                    {
                        if (int_array[j] < int_array[min])        //并求得其中的最小值,若找到则min=j
                            min = j;
                    }
                    (int_array[i], int_array[min]) = (int_array[min], int_array[i]);//组元交换,最小值与当前行的第i个元素交换,其它元素位置不变
                }
                textBox2!.Clear();
                foreach (int i in int_array)
                {
                    int index = Array.IndexOf(int_array, i);
                    if (index != int_array.GetUpperBound(0))
                    {
                        textBox2.Text += i.ToString() + ", ";
                    }
                    else
                        textBox2.Text += i.ToString();
                }
            }
            else
            {
                MessageBox.Show("首先应当生成数组,然后再进行排序。", "提示!");
            }
        }
    }
}

2.生成效果 

 C#一维数组排序方法:选择排序法_第1张图片

你可能感兴趣的:(排序算法,数据结构,算法)