Merge Sort

Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.

Merge(A, left, mid, right)
n1 = mid - left;
n2 = right - mid;
create array L[0...n1], R[0...n2]
for i = 0 to n1-1
do L[i] = A[left + i]
for i = 0 to n2-1
do R[i] = A[mid + i]
L[n1] = SENTINEL
R[n2] = SENTINEL
i = 0;
j = 0;
for k = left to right-1
if L[i] <= R[j]
then A[k] = L[i]
i = i + 1
else A[k] = R[j]
j = j + 1 Merge-Sort(A, left, right){
if left+1 < right
then mid = (left + right)/2;
call Merge-Sort(A, left, mid)
call Merge-Sort(A, mid, right)
call Merge(A, left, mid, right)

Input

In the first line n is given. In the second line, n integers are given.

Output

In the first line, print the sequence S. Two consequtive elements should be separated by a space character.

In the second line, print the number of comparisons.

Constraints

  • n ≤ 500000
  • 0 ≤ an element in S ≤ 109

Sample Input 1

10
8 5 9 2 6 3 7 1 10 4

Sample Output 1

1 2 3 4 5 6 7 8 9 10
34
又抄了一份题解(那个34是归并排序比较的次数)代码如下
#include<iostream>
#include<cstring>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 500000
#define INF 2e9
int L[MAX/+],R[MAX/+];
int cnt;
void merge(int A[],int n,int left,int mid,int right)
{
int n1=mid-left;
int n2=right-mid;
for(int i=;i<n1;i++)
{
L[i]=A[left+i];
}
for(int i=;i<n2;i++)
{
R[i]=A[mid+i];
}
L[n1]=INF;
R[n2]=INF;
int i=,j=;
for(int k=left;k<right;k++)//合并
{
cnt++;
if(L[i]<=R[j])
A[k]=L[i++];
else
A[k]=R[j++];
}
}
void mergeSort(int A[],int n,int left,int right)
{
if(left+<right)
{
int mid=(left+right)/;
mergeSort(A,n,left,mid);
mergeSort(A,n,mid,right);
merge(A,n,left,mid,right);
}
}
int main()
{
int A[MAX],n;
cnt=;
cin>>n;
for(int i=;i<n;i++)
cin>>A[i];
mergeSort(A,n,,n);
for(int i=;i<n;i++)
{
if(i)
cout<<" ";
cout<<A[i];
}
cout<<endl;
cout<<cnt<<endl;
return ;
}

归并排序 ALDS1_5_B:Merge Sort的更多相关文章

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

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

  2. 【高级排序算法】1、归并排序法 - Merge Sort

    归并排序法 - Merge Sort 文章目录 归并排序法 - Merge Sort nlogn 比 n^2 快多少? 归并排序设计思想 时间.空间复杂度 归并排序图解 归并排序描述 归并排序小结 参 ...

  3. 【算法】归并排序(Merge Sort)(五)

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

  4. 归并排序(Merge Sort)

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

  5. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  6. 【DS】排序算法之归并排序(Merge Sort)

    一.算法思想 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法的一个非常典型的应用,指的是将两个已经排序的序列合并成一个序列的操作.其归并思想如下: 1)申请空间,使其大小为两个已经 ...

  7. 自底向上归并排序(Merge Sort)

    一.思路 另一种实现归并排序的方法是,先归并微型数组,再成对归并得到的子数组,直到将整个数组归并在一起. 我们先进行1-by-1归并,然后2-by-2归并,4-by-4归并,如此下去. 在最后一次归并 ...

  8. 自顶向下归并排序(Merge Sort)

    一.自顶向下的归并排序思路: 1.先把数组分为两个部分. 2.分别对这两个部分进行排序. 3.排序完之后,将这两个数组归并为一个有序的数组. 重复1-3步骤,直到数组的大小为1,则直接返回. 这个思路 ...

  9. 【Algorithms】归并排序(merge sort)

    几个比较常见的排序算法里头,这个我是比较生疏的一个,有一天突然被问了一个问题是,归并排序最大的特点是什么,我才想起这个算法来.下午又看不进书啦,就实现一下,记下来. 归并排序采取的是分治策略,就是先将 ...

随机推荐

  1. css沉默

    css变色龙实现. ==== css 1 水平居中和垂直居中. 2 css布局方式. 3 你写过UI框架么.

  2. javascript中onclick(this)用法介绍

    this指触发事件的对象 代码如下: <input id="myinput" type="text" value="javascript中onc ...

  3. Winfrom 减少控件重绘闪烁的方法

    Winform控件的双缓冲.控件的双缓冲属性是隐藏的,可以通过反射改变其属性值. lv.GetType().GetProperty("DoubleBuffered", Bindin ...

  4. Binder基本使用

    Android开发中,Binder是一种跨进程通信方式,而使用AIDL可以实现Binder的工作. 如何使用它是了解它的第一步,本文章主要记录使用Binder的一些步骤.(代码思路参考<Andr ...

  5. VEH帮你定位程序崩溃地址

    之前朋友有一个服务端程序,总是受到一些人的恶意漏洞攻击,没有源代码,只好反汇编修复了漏洞,并且使用WinLicense加保护授权. 漏洞总不是一次可以修复完的,恶意攻击并没有停止,然后加了WL保护程序 ...

  6. #《Essential C++》读书笔记# 第五章 面向对象编程风格

    基础知识 继承机制定义了父子(parent/child)关系.父类(parent)定义了所有子类(children)共通的共有接口(public interface)和私有实现(private imp ...

  7. 使用Ajax时[object%20object] 报错的解决方案

    踩坑经过 最近初学Ajax,当我想把Ajax应用到自己项目中的时候,没有达到理想的效果,还报了如下错误: 点击图中报错,产生报错页面如下: 当时写的Ajax如下: // 提交修改密码表单 $(&quo ...

  8. html基本标签表单实现交互原理,单选框,复选框,下拉框介绍

    表单是什么?表单是前端和服务器做交互的一种机制,表单收集用户输入信息,之后发送或者提交给服务器.用户在输入的信息称之为内容,内容的文本分为普通和密码型,用户通过单选框.复选框.下拉框(也就是下拉菜单) ...

  9. Eclipse中Git图标表示内容

    Eclipse中->属性->Team->Git->Label Decorations

  10. mysql 表结构操作

    alter table name : alter table table1 to table2;add column : alter table 表名 add column 列名 varchar(); ...