Insertion sort

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( " \nInsert data: " );
             int x= int .Parse(Console.ReadLine());
            DirectlyInsert(num, x);
            DichotomyInsertSort(num, x);
            Console.ReadKey();
        }
        // Automatically generate test data 
        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> 
        /// Direct insertion sort
         ///  </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( " Insert Sort: " );
            ShowArrayValues ​​(endArray);
        }
       
        ///  <summary> 
        /// Half insertion sort (binary insertion sort)
         ///  </summary> 
        static  void DichotomyInsertSort( int [] num, int target)
        {
            // Copy the original array, the first value of the new array is the value to be inserted, followed by 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( " \nBiary Insertion Sort: " );     
             int low = 1 , mid, high = num.Length;
             int index = 0 ;
             // 1. When the target value is between the minimum and maximum values
             ​​// 2. When the target value is less than the minimum value
             // 3. When the target value is greater than the minimum value 
            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;
                }
            }
            // left shift left 
            for ( int i = 0 ; i <index; i++ )
            {
                endArray [i] = endArray [i + 1 ];
            }
            endArray[index] = target;
            ShowArrayValues ​​(endArray);
           // int low=0, mid, high=num; 
        }
         ///  <summary> 
        /// Display array data
         ///  </summary> 
        ///  <param name="values"></param> 
        static  void ShowArrayValues( int [] values)
        {
            foreach (int item in values)
            {
                Console.Write(item + " ");
            }
        }

        ///  <summary> 
        /// Sort from small to large
         ///  </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;
        }
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324518348&siteId=291194637