The principle and code implementation of C# merge sort

Off-topic: oh well, one day liver 4 chapters, yes you boy


1. What is merge sort

  • Merge two or more ordered subsequences into one ordered sequence .
  • You can understand it as, when I was in junior high school, I had a handicraft class, and the teacher said that two students should be in a group;
    • Every 2 of us act as a group and choose the best work;
    • Then the teacher said: Every two combinations are combined into a 4-person combination , and then the best work is selected;
    • Next, every two combinations are merged into an 8-person combination until one of the best works is selected.

PS: The above example is not rigorous, just to let you understand the process of "merging" .


2. 2-way merge sort

  • What is 2-way merge sort?
    • As mentioned above, the merge sort is " 2 or more sequences xxxxx";
    • Then we only choose 2 sequences and 2 sequences to merge , then it is 2-way merge sort .
  • picture explanation
    insert image description here

3. 2-way merge sort code implementation

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

namespace YoyoCode
{
    
    
    internal class MSort
    {
    
    
        //辅助数组
        static int[] sArray;
        /// <summary>
        /// 归并排序
        /// </summary>
        /// <param name="pArray">需要排序的数组</param>
        public static void MergeSort(int[] pArray)
        {
    
    
            //辅助数组的初始化
            sArray = new int[pArray.Length];
            //步长限制1、2、4、8、16...
            //如果数组长度为9,最大步长不能为4,必须是8
            //因为8和剩下的一个元素需要合并
            for (int step = 1; step < pArray.Length; step *= 2)
            {
    
    
                //在步长限制下,对数组的两两归并
                for (int j = 0; j < pArray.Length - step; j += step + step)
                {
    
    
                    Merge(pArray, j, j + step - 1, Math.Min(j + step + step - 1, pArray.Length - 1));
                }
            }
        }
        /// <summary>
        /// 对两个子数组进行排序
        /// </summary>
        /// <param name="pArray">数组</param>
        /// <param name="low">子数组1的起始index</param>
        /// <param name="mid">子数组1的终止index,mid+1就是子数组2的起始index</param>
        /// <param name="high">子数组2的终止index</param>
        private static void Merge(int[] pArray, int low, int mid, int high)
        {
    
    
            //辅助数组赋值
            for (int k = low; k <= high; k++)
            {
    
    
                sArray[k] = pArray[k];
            }
            //合并
            int i = low; 
            int j = mid + 1;
            for (int k = low; k <= high; k++)
            {
    
    
                //越界直接合并
                if (i > mid)
                {
    
    
                    pArray[k] = sArray[j++];
                }
                else if (j > high)
                {
    
    
                    pArray[k] = sArray[i++];
                }
                else
                {
    
    
                    //没越界则比较,两者中小的放进结果中
                    if (sArray[i] < sArray[j])
                    {
    
    
                        pArray[k] = sArray[i++];
                    }
                    else
                    {
    
    
                        pArray[k] = sArray[j++];
                    }
                }
            }
        }
    }
}

Explanation: It's all in the wine, ah no, it's all in the notes, read the notes, and then rub your hands together to understand the principle, it just looks disgusting~


4. Algorithm analysis

  • time complexity
    • O(nlogn)
  • space complexity
    • O(n)
  • algorithm stability
    • Stablize


Conclusion: so tired, so tired, so tired

Guess you like

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