插入排序

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;
        }
    }
}

 

posted @ 2018-04-16 20:19  游戏鼻祖  阅读(166)  评论(0编辑  收藏  举报