归并排序(Merge sort,台湾译作:合并排序)

来源:互联网 发布:java二叉树递归高度 编辑:程序博客网 时间:2024/06/05 13:43
归并排序(Merge sort,台湾译作:合并排序)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用


归并排序
Example of merge sort sorting a list of random dots.
一个归并排序的例子:对一个随机点的链表进行排序分类排序算法数据结构数组最差时间复杂度\Theta(n\log n)最优时间复杂度\Theta(n)平均时间复杂度\Theta(n\log n)最差空间复杂度\Theta(n)最佳算法有时是


归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并排序算法依赖归并操作。

[编辑]算法描述

归并操作的过程如下:

  1. 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
  2. 设定两个指针,最初位置分别为两个已经排序序列的起始位置
  3. 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
  4. 重复步骤3直到某一指针达到序列尾
  5. 将另一序列剩下的所有元素直接复制到合并序列尾

C#语言

        public static List<int> sort(List<int> lst)        {            if (lst.Count <= 1)            {                return lst;            }            int mid = lst.Count / 2;            List<int> left = new List<int>();//定义左侧List            List<int> right = new List<int>();//定义右侧List             //以下兩個循環把lst分為左右兩個List            for (int i = 0; i < mid; i++)            {                left.Add(lst[i]);            }            for (int j = mid; j < lst.Count; j++)            {                right.Add(lst[j]);            }            left = sort(left);            right = sort(right);            return merge(left, right);        }        /// <summary>        /// 合併兩個已經排好序的List        /// </summary>        /// <param name="left">左側List</param>        /// <param name="right">右側List</param>        /// <returns></returns>        static List<int> merge(List<int> left, List<int> right)        {            List<int> temp = new List<int>();            while (left.Count > 0 && right.Count > 0)            {                if (left[0] <= right[0])                {                    temp.Add(left[0]);                    left.RemoveAt(0);                }                else                {                    temp.Add(right[0]);                    right.RemoveAt(0);                }            }            if (left.Count > 0)            {                for (int i = 0; i < left.Count; i++)                {                    temp.Add(left[i]);                }            }            if (right.Count > 0)            {                for (int i = 0; i < right.Count; i++)                {                    temp.Add(right[i]);                }            }            return temp;        }

[编辑]

归并排序

归并排序具体工作原理如下(假设序列共有n个元素):

  1. 将序列每相邻两个数字进行归并操作,形成floor(n/2)个序列,排序后每个序列包含两个元素
  2. 将上述序列再次归并,形成floor(n/4)个序列,每个序列包含四个元素
  3. 重复步骤2,直到所有元素排序完毕

void merge_sort(int array[], unsigned int first, unsigned int last){        int mid = 0;        if(first<last)        {                /*mid = (first+last)/2;*/ /*注意防止溢出*/                /*mid = first/2 + last/2;*/                /*mid = ((first & last) + (first ^ last) >> 1);*/                mid = ((first & last) + ((first ^ last) >> 1));    /*修正上一句优先级错误*/                merge_sort(array, first, mid);                merge_sort(array, mid+1,last);                merge(array,first,mid,last);        }}


算法复杂度

比较操作的次数介于(n log n)/2n log n - n + 1。 赋值操作的次数是(2nlogn)。 归并算法的空间复杂度为:Θ (n)


原创粉丝点击