插入排序

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

namespace InsertAlgorithm
{
    class Program
    {
        static int[] num = new int[10];
        static void Main(string[] args)
        {
            InitData();
            num=Sort(num);
            ShowArrayValues(num);
            Console.WriteLine("\n插入数据:");
            int x=int.Parse(Console.ReadLine());
            DirectlyInsert(num, x);
            DichotomyInsertSort(num, x);
            Console.ReadKey();
        }
        //自动生成测试数据
        static void InitData()
        {
            for (int i = 0; i < num.Length; i++)
            {

                var Seed = Guid.NewGuid().GetHashCode();
                var value = new Random(Seed);
                num[i] = value.Next(1, 100);
            }
        }
        /// <summary>
        /// 直接插入排序
        /// </summary>
        static void DirectlyInsert(int[] num,int target)
        {
            int[] endArray = new int[num.Length + 1];
            endArray[0] = target;
            for (int i = 1; i < endArray.Length; i++)
            {
                endArray[i] = num[i - 1];
            }
            for (int i = 0; i < endArray.Length-1; i++)
            {
                if(endArray[i]>endArray[i+1])
                {
                    int temp = endArray[i];
                    endArray[i] = endArray[i+1];            
                    endArray[i + 1] = temp;
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("直接插入排序:");
            ShowArrayValues(endArray);
        }
       
        /// <summary>
        /// 折半插入排序(二分插入排序)
        /// </summary>
        static void DichotomyInsertSort(int[] num, int target)
        {
            //将原数组复制一遍,新数组首位是欲插入值,后面是endArray[i+1]=num[i]
            int[] endArray =new int[num.Length+1]; 
            for (int i = 0; i < num.Length; i++)
            {
                endArray[i+1] = num[i];
            }
            endArray[0] = target;
            Console.WriteLine("\n二分插入排序:");     
            int low = 1, mid, high = num.Length;
            int index = 0;
            //1.当目标值位于最小值与最大值中间
            //2.当目标值小于最小值
            //3.当目标值大于最小值
            while(low<=high)
            {
                mid=low+((high-low)/2);
                if (endArray[low] <= target && endArray[mid] >= target)
                {
                    high = mid;
                }
                else if (endArray[mid] < target && endArray[high] >= target)
                {
                    low = mid;
                }
                else if (endArray[low] > target)
                {
                    index = low - 1;
                    break;
                }
                else if (endArray[high] < target)
                {
                    index = high;
                    break;
                }
                if (low + 1 == high)
                {
                    index = low;
                    break;
                }
            }
            //左边左移
            for (int i = 0; i <index; i++)
            {
                endArray[i ] = endArray[i+ 1];
            }
            endArray[index] = target;
            ShowArrayValues(endArray);
           // int low=0, mid, high=num;
        }
        /// <summary>
        /// 显示数组的数据
        /// </summary>
        /// <param name="values"></param>
        static void ShowArrayValues(int[] values)
        {
            foreach (int item in values)
            {
                Console.Write(item + " ");
            }
        }

        /// <summary>
        /// 从小到大排序
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private static int[] Sort(int[] i)
        {
            List<int> list = i.ToList();
            list.Sort();
            i= list.ToArray<int>();
            return i;
        }
    }
}

 

转载于:https://www.cnblogs.com/lichuangblog/p/8858292.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值