C# Insertion sorting | Direct insertion sorting | Binary insertion sorting | The principle and code implementation of Hill sorting

Digression : I caught a cold, and after a few days of dove, I was revived with full blood!


1. What is insertion sort?

  • Imagine you are playing poker, and now you are drawing cards;
  • You have drawn a 7. In order to play cards more comfortably, we usually choose to sort the cards in our hands in a certain order, such as from small to large;
  • Then this 7 needs to be inserted between 5 and 10 after sorting ;
  • You keep drawing cards and inserting new cards into your hand to ensure the correct order;
  • This process is insertion sort .

2. Direct insertion sort

  • What is direct insertion sort?

    • Same as above example, take one and insert one ;
    • Moreover, when looking for the insertion position, you choose a card for size comparison ;
    • This process is direct insertion sort .
  • Code

        /// <summary>
        /// 直接插入排序
        /// </summary>
        /// <param name="pArray">索引从1开始有效的数组</param>
        static public void DirectSort(int[] pArray)
        {
          
          
            int i, j;
            for (i = 1; i < pArray.Length; i++)
            {
          
          
                //设置哨兵
                pArray[0] = pArray[i];
                //寻找插入点的同时,向后移动元素
                for (j = i - 1; pArray[j] > pArray[0]; j--)
                    pArray[j + 1] = pArray[j];
                //插入元素
                pArray[j + 1] = pArray[0];
            }
        }
    
  • time complexity

    • Best case: O(n)
    • Worst case: O(n^2)
    • Average: O(n^2)
  • Features:

    • The more ordered the original data , the higher the performance of direct plug-in , the closer to O(n) .
    • PS: The "ordered" here refers to the same direction as the sorted results , for example, from small to large or from large to small.
  • space complexity

    • O(1)
  • algorithm stability

    • Stablize

3. Binary insertion sort

  • What is binary insertion sort?

    • Similar to direct insertion, each card is inserted into the hand one by one ;
    • However , when looking for the insertion position, a binary search is used to determine the insertion position .
  • Code

        /// <summary>
        /// 二分插排
        /// </summary>
        /// <param name="pArray">索引从1开始有效的数组</param>
        static public void BinarySort(int[] pArray)
        {
          
          
            int low, high, mid;
            for (int i = 1; i < pArray.Length; i++)
            {
          
          
                //设置哨兵
                pArray[0] = pArray[i];
                //二分查找双指针设置
                low = 1;
                high = i - 1;
                //二分查找
                while (low <= high)
                {
          
          
                    mid = (low + high) / 2;
                    if (pArray[mid] > pArray[0])
                    {
          
          
                        high--;
                    }
                    else
                    {
          
          
                        low++;
                    }
                }
                //逐位向后移动
                for (int j = i; j > high + 1; j--)
                {
          
          
                    pArray[j] = pArray[j - 1];
                }
                //high + 1是需要插入的位置
                pArray[high + 1] = pArray[0];
            }
        }
    
  • time complexity

    • Best case: O(n^2)
    • Worst case: O(n^2)
    • Although the magnitude is the same as that of direct plug-in, it is better in terms of average performance.
  • space complexity

    • O(1)
  • algorithm stability

    • Stablize

4. Hill sort

  • Why is there a Hill sort?

    • In "Direct Insertion", we talked about " the more orderly the original data, the higher the performance ";
    • Following this line of thinking, "Can we make the data roughly orderly before sorting, so as to improve performance?"
    • Eh! ! Hill sorted him to come, that's the idea.
  • The idea of ​​Hill sorting

    • We set a step size, such as 5, and perform direct insertion between elements with an interval of 5, such as sorting the three elements of 1, 6, and 11, sorting the three elements of 2, 7, and 12, and so on.
    • Let's set a step size, 3, between the elements with an interval of 3, and insert and arrange ;
    • Finally, we set the step size to 1 , which is the ordinary direct insertion;
    • to get the final result.
  • About the setting of the step size

  • Code

    	/// <summary>
        /// 希尔排序
        /// </summary>
        /// <param name="pArray">索引从1开始有效的数组</param>
        /// <param name="delta">互质递减序列</param>
        static public void ShellSort(int[] pArray, int[] delta)
        {
          
          
            //对互质递减序列中,每个step进行一次排序
            for (int i = 0; i < delta.Length; i++)
            {
          
          
                ShellInsert(pArray, delta[i]);
            }
        }
        static public void ShellInsert(int[] pArray, int step)
        {
          
          
            int i, j;
            //从step + 1开始,一个元素一个元素进行普通的直接插排
            //为什么从step + 1开始?从1-step的这step个元素,并不需要走一遍循环,因为它们自身所在的排序序列中,只有他们自己
            //而从step + 1开始,可以照顾到所有的元素会进行插排,且保证每个元素前面都至少有一个元素可以进行比较。
            for (i = step + 1; i < pArray.Length; i++)
            {
          
          
                //设置哨兵
                pArray[0] = pArray[i];
                //寻找插入点的同时,向后移动元素
                for (j = i - step; j > 0 && pArray[j] > pArray[0]; j = j - step)
                    pArray[j + step] = pArray[j];
                pArray[j + step] = pArray[0];
            }
        }
    }
    
  • time complexity

    • Best case: O(n)
    • Worst case: O(n^2)
    • Average: O(n^1.3)
  • space complexity

    • O(1)
  • algorithm stability

    • unstable


Conclusion: These algorithms look very troublesome, but after writing them, I found that they are quite simple, hahaha

Guess you like

Origin blog.csdn.net/Liyager/article/details/129204976