c#常见算法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyPro
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] arrayRand = new int[10];
        private void btnRandomArray_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            for (int i = 0; i < arrayRand.Length; i++)
            {
                arrayRand[i] = r.Next(0, 101);
            }
            ShowArrayToUI(rtxtRand, arrayRand);
        }
        private void ShowArrayToUI(RichTextBox t, int[] array)
        {
            string text = "";
            for (int i = 0; i < array.Length; i++)
            {
                text += array[i].ToString() + "\n";
            }
            t.Text = text;
        }
        /// <summary>
        /// 冒泡排序
        /// </summary>
        private void btnMaopaoSort_Click(object sender, EventArgs e)
        {
            int[] array = new int[arrayRand.Length];
            Array.Copy(arrayRand, array, arrayRand.Length);
            int temp;
            for (int i = array.Length - 1; i > 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
            ShowArrayToUI(this.rtxtSort, array);
        }
        /// <summary>
        /// 二分法排序
        /// </summary>
        private void btnMidSort_Click(object sender, EventArgs e)
        {
            int[] array = new int[arrayRand.Length];
            Array.Copy(arrayRand, array, arrayRand.Length);
            int temp;
            int start, end, mid;
            for (int i = 0; i < array.Length; i++)
            {
                start = 0;
                end = i - 1;
                temp = array[i];
                while (start <= end)
                {
                    mid = (start + end) / 2;
                    if (array[mid] < temp)
                    {
                        start = mid + 1;
                    }
                    else
                    {
                        end = mid - 1;
                    }
                }
                for (int j = i - 1; j > end; j--)
                {
                    array[j + 1] = array[j];
                }
                array[end + 1] = temp;
            }
            ShowArrayToUI(this.rtxtSort, array);
        }
        /// <summary>
        /// 插入法排序  每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止
        /// </summary>
        private void btnInsertSort_Click(object sender, EventArgs e)
        {
            int[] array = new int[arrayRand.Length];
            Array.Copy(arrayRand, array, arrayRand.Length);
            int temp;
            int counter;
            for (int i = 1; i < array.Length; i++)
            {
                counter = i;
                temp = array[i];
                while (counter > 0 && (array[counter - 1] > temp))
                {
                    array[counter] = array[counter - 1];
                    counter--;
                }
                array[counter] = temp;
            }
            ShowArrayToUI(this.rtxtSort, array);
        }
        /// <summary>
        /// 选择排序  选择排序是从冒泡排序演化而来的,每一轮比较得出最小的那个值,然后依次和每轮比较的第一个值进行交换。 
        /// </summary>
        private void btnChooseSort_Click(object sender, EventArgs e)
        {
            int[] array = new int[arrayRand.Length];
            Array.Copy(arrayRand, array, arrayRand.Length);
            int temp;
            int tempIndex;
            for (int i = 0; i < array.Length; i++)
            {
                temp = array[i];
                tempIndex = i;
                //找出最小的一个
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[j] < temp)
                    {
                        temp = array[j];
                        tempIndex = j;
                    }
                }
                //交换位置
                temp = array[i];
                array[i] = array[tempIndex];
                array[tempIndex] = temp;
            }
            this.ShowArrayToUI(this.rtxtSort, array);
        }
    }
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号