Iterative (non-recursive) Merge Sort
An iterative way of writing merge sort:
#include <iostream>
using namespace std; void merge(int A[], int l, int r, int e, int B[]) {
int i = l, j = r, k = l;
while (i<r && j<e) {
if (A[i] > A[j]) B[k++] = A[j++];
else B[k++] = A[i++];
}
while (i < r) B[k++] = A[i++];
while (j < e) B[k++] = A[j++];
} void mergeSort(int A[], int n) {
int *B = new int[n];
for (int w=1; w<n; w<<=1) {
for (int i=0; i<n; i+=(w<<1)) {
merge(A, i, min(i+w, n), min(i+(w<<1), n), B);
}
for (int i=0; i<n; ++i) A[i] = B[i];
}
delete[] B;
} int main() {
int A[5] = {5,4,3,2,1};
mergeSort(A, 5);
for (int i=0; i<5; ++i) cout<<A[i]<<" ";
return 0;
}
Iterative (non-recursive) Merge Sort的更多相关文章
- Summary: Merge Sort of Array && 求逆序对
常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...
- 873D. Merge Sort
Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a w ...
- 复杂度分析 quick sort&merge sort
空间复杂度看新开了什么数据结构就够了 公式=几个点*每个点执行了多少次 二叉树都是n次 二分法查找:lgn 全部查找:n n:找一个数,但是两边都要找.相当于遍历.类似于rotated sorted ...
- 【算法】转载:Iterative vs. Recursive Approaches
Iterative vs. Recursive Approaches Eyal Lantzman, 5 Nov 2007 CPOL Introduction This arti ...
- 【CF edu 30 D. Merge Sort】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript
Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- 归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...
随机推荐
- xen save/restore 过程
以下分析基于 xen4.2.3, 虚拟机都是hvm模式 使用libxl库有两种方式启动一个虚拟机,一种是 xl create xx.conf , 这种方式从一个配置文件开始启动一个虚拟机,速度相对较慢 ...
- Linux 设备驱动--- 阻塞型字符设备驱动 --- O_NONBLOCK --- 非阻塞标志【转】
转自:http://blog.csdn.net/yikai2009/article/details/8653697 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 阻塞 阻 ...
- pythontips(1):打印模块的属性并执行
import sys import site def print_all(module_): modulelist = dir(module_) length = len(modulelist) fo ...
- python每日一类(3):os和sys
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...
- Laravel实现用户名或密码登录
要实现用户名或密码登录,这就要用到强大的filter_var函数 该函数通过指定的过滤器过滤变量,可以判断输入值是否是数字.是否是字符串.是否是邮箱.是否是IP等等,不用写麻烦的正则 $type = ...
- (8)C#字符串
一.字符串 为什么说string是一个不可变的字符序列. string a="me"; a="meeeee"; string b="me" ...
- spoj 913 Query on a tree II (倍增lca)
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...
- hashmap hashtable
作者:付佳豪链接:https://zhuanlan.zhihu.com/p/37607299来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 在面试的时候,java集合最 ...
- [BZOJ 1266] 上学路线Route
Link: BZOJ 1266 传送门 Solution: 好不容易自己写出来一道水题,练链式前向星的模板调了一小时o(╯□╰)o 思路非常好想,既然要想让最短路不成立,使最短路部分不连通即可 又要求 ...
- POJ 2987 Firing(最大权闭合图)
[题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...