C#冒泡排序、选择排序、插入排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace no2_4
{
    class Program
    {
        static void Main(string[] args)
        {
           // mpao();
            //szpx();
            inset();
        }
       static int[] arry = new int[5];
        private static void xu()
        {
           // int[] arry = new int[5];
            Console.WriteLine("请输入5个整数");
            for (int i = 0; i < arry.Length; i++)
            {
                arry[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("排序前的顺序是:");
            foreach (var item in arry)
            {
                Console.Write(item + "\t");
            }
        }
        /// 
        /// 冒泡排序
        /// 
        private static void mpao()
        {
            xu();
            for (int i = 0; i < arry.Length - 1; i++)
            {
                for (int j = arry.Length - 1; j > i; j--)
                {
                    if (arry[i] > arry[j])
                    { 
                        int temp=arry[i];
                        arry[i]=arry[j];
                        arry[j] = temp;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("排序后为:");
            foreach (int v in arry)
            {
                Console.Write(v+"\t");
            }
            Console.WriteLine();
            Console.ReadLine();
            

        }
        /// 
        /// 选择排序
        /// 
        private static void szpx()
        {
            xu();

            for (int i = 0; i < arry.Length - 1; i++)
            {
                int min = arry[i];
                int minIndex = i;
                for (int j = i; j <= arry.Length - 1; j++)
                {
                    if (min > arry[j])
                    { 
                        min=arry[j];
                        minIndex = j;
                    }
                }
                int temp=arry[i];
                arry[i]=arry[minIndex];
                arry[minIndex] = temp;
            }
                Console.WriteLine();
            Console.WriteLine("排序后为:");
            foreach (int v in arry)
            {
                Console.Write(v + "\t");
            }
            Console.WriteLine();
            Console.ReadLine();
        }
        /// 
        /// 插入排序
        /// 
        private static void inset()
        {
            xu();
            for (int i = 1; i < arry.Length; i++)
            { 
                int temp=arry[i];
                int insert = i;
                for (int j = i; j > 0; j--)
                {
                    if (arry[j - 1] > temp)
                    { 
                        arry[j]=arry[j-1];
                        insert = j - 1;
                    }
                }
                arry[insert] = temp;
            }
                Console.WriteLine("排序后为:");
            foreach (int v in arry)
            {
                Console.Write(v + "\t");
            }
            Console.WriteLine();
            Console.ReadLine();
            
        }
    }
}

你可能感兴趣的:(C#控制台,c#)