归并排序运行时间O(N log N),但是由于需要线性附加内存,所以很少用于主存排序。

  算法核心在于以下三条语句,分治递归,分别对左半边和右半边的数组进行排序,然后把左右半边的数组一一进行比较放入数组

         msort(nums,tmp,lp,center);
msort(nums,tmp,center+,rp);
merge(nums,tmp,lp,center+,rp);

下面是代码,主要包括三个函数:

 void mergesort(int *nums,int n)
{
int *tmp=new int[n];
if(tmp)
{
msort(nums,tmp,,n-);
delete[]tmp;
}
}
void msort(int *nums,int*tmp,int lp,int rp)
{
int center=(lp+rp)/;
if(lp<rp)
{
msort(nums,tmp,lp,center);
msort(nums,tmp,center+,rp);
merge(nums,tmp,lp,center+,rp);
}
}
void merge(int *nums,int *tmp,int lp,int rp,int over)
{
int i=lp,j=rp,p=lp;
while(i<rp&&j<=over)
{
if(nums[i]<nums[j])
tmp[p++]=nums[i++];
else
tmp[p++]=nums[j++];
}
while(i<=rp-)
tmp[p++]=nums[i++];
while(j<=over)
tmp[p++]=nums[j++];
while(lp<=over)
{
nums[lp]=tmp[lp];
lp++;
}
}

【Sort】Merge Sort归并排序的更多相关文章

  1. [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)

    Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...

  2. 复杂度分析 quick sort&merge sort

    空间复杂度看新开了什么数据结构就够了 公式=几个点*每个点执行了多少次 二叉树都是n次 二分法查找:lgn 全部查找:n n:找一个数,但是两边都要找.相当于遍历.类似于rotated sorted ...

  3. sicily 1154. Easy sort (tree sort& merge sort)

    Description You know sorting is very important. And this easy problem is: Given you an array with N ...

  4. Sort - Merge Sort

    归并排序思想 归并排序时间复杂度分析 归并排序特点 归并排序实现 递归 #include <iostream> using namespace std; void merge(int *a ...

  5. [算法]——归并排序(Merge Sort)

    归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...

  6. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  7. 经典排序算法 - 归并排序Merge sort

    经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...

  8. 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)

    连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...

  9. 排序算法二:归并排序(Merge sort)

    归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...

  10. 归并排序 ALDS1_5_B:Merge Sort

    Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...

随机推荐

  1. C#文件处理

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. C# LocalizationHelpers (搬运)

    using Utils; namespace System.Web.Mvc { /// <summary> /// 本地化帮助类 /// </summary> public s ...

  3. Ubuntu 14.04—Anaconda 相关

    Anaconda 使用国内镜像: https://keyunluo.github.io/2016/07/17/2016-07-17-anaconda-python.html Anaconda下的 De ...

  4. 驱动7段LED显示器

    拿到7段LED显示器,先看看是共阴极还是共阳极,如果是共阳极,3和8接5V,5V串联一个220欧姆的电阻. 下面是购买的LED显示器的接线图例 5V串联电阻图例 下面为代码,此代码将实现在LED显示器 ...

  5. web字体图标的使用

    今天给大家介绍一些web字体图标的下载和使用 一.WEB字体 1. 下载外部的字体图标的网站 font-awesome.com 2.CSS文件和font文件 3.html文档中使用外部字体 4.下载字 ...

  6. Markdown引用本地图片语法

    Markdown引用本地图片语法 markdown引用图片标准方式如下: ![Alt text](/path/to/img.jpg) 测试markdown文本如下: # 测试相对路径图片 ![Alt ...

  7. 1951: [Sdoi2010]古代猪文

    1951: [Sdoi2010]古代猪文 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2171  Solved: 904[Submit][Status] ...

  8. dplyr 数据操作 常用函数(5)

    继续来了解dplyr中的其他有用函数 1.sample() 目的是可以从一个数据框中,随机抽取一些行,然后组成新的数据框. sample_n(tbl, size, replace = FALSE, w ...

  9. window下安装apache---使用wamp

    01 wamp-server-wamp5-2-5-multi-win.exe 02 wamp报错时,需要的补丁(vcredist_x64.exe) 无法启动此程序,因为计算机中丢失MSVCR110.d ...

  10. 【转】delphi 保存到txt文件

    procedure TForm1.btn1Click(Sender: TObject); var astr: string; sList: TStrings; path: string; begin ...