【Sort】Merge Sort归并排序
归并排序运行时间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归并排序的更多相关文章
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...
- 复杂度分析 quick sort&merge sort
空间复杂度看新开了什么数据结构就够了 公式=几个点*每个点执行了多少次 二叉树都是n次 二分法查找:lgn 全部查找:n n:找一个数,但是两边都要找.相当于遍历.类似于rotated sorted ...
- 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 ...
- Sort - Merge Sort
归并排序思想 归并排序时间复杂度分析 归并排序特点 归并排序实现 递归 #include <iostream> using namespace std; void merge(int *a ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- 连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort)
连续线性空间排序 起泡排序(bubble sort),归并排序(merge sort) 1,起泡排序(bubble sort),大致有三种算法 基本版,全扫描. 提前终止版,如果发现前区里没有发生交换 ...
- 排序算法二:归并排序(Merge sort)
归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是O(nlgn). (一)算法实现 private void merge_sort(int[] array, int ...
- 归并排序 ALDS1_5_B:Merge Sort
Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...
随机推荐
- cocos2d基本类介绍 director/scene/layer/sprite
[核心类] 导演Director.场景Scene.布景层Layer.精灵Sprite的概念请移步: 导演控制场景,场景控制图层,图层控制精灵,精灵控制动作. 相互之间的关系框架 ...
- Android学习笔记(二)Git和Github
一.添加SSH Key ssh-keygen -t rsa -C "email@example.com" 遇到提示只需要一直确认.用户目录(如/root)下会生成一个.ssh文件夹 ...
- Redis的安装和使用之------Redis相关运用
原文 http://wangzhijian.blog.51cto.com/6427016/1731962 一.简介 REmote DIctionary Server(Redis) 是一个由 Salv ...
- Unity灯光详解
Lights will bring personality and flavor to your game. You use lights to illuminate the scenes and o ...
- Java 四舍五入并小数点后保存两位,千分位分隔
import java.text.DecimalFormat; public class FileTest { public static void main(String[] args) { ...
- CodeForces 669E Little Artem and Time Machine
树状数组,$map$. 可以理解为开一个数组$f[i][j]$记录:$i$这个数字在时间$j$的操作情况. 操作$1$:$f[x][t]++$.操作$2$:$f[x][t]--$.操作$3$:$f[x ...
- 第一百二十一节,JavaScript事件绑定及深入
JavaScript事件绑定及深入 学习要点: 1.传统事件绑定的问题 2.W3C事件处理函数 3.IE事件处理函数 4.事件对象的其他补充 事件绑定分为两种:一种是传统事件绑定(内联模型,脚本模型) ...
- [SQL基础教程] 3-3 HAVING
[SQL基础教程] 3-3 HAVING HAVING子句 SELECT col_1,col_2 FROM table GROUP BY col_1,col_2 HAVING col_1 = '2'; ...
- IBM Minus One
IBM Minus One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- mysql安装和基本配置-redhat
1.redhat yum替换参考 url:http://blog.csdn.net/zcyhappy1314/article/details/17580943 2.yum卸载mysql rpm -qa ...