Summary: Merge Sort of Array && 求逆序对
常用算法(后面有inplace版本):
package ArrayMergeSort; import java.util.Arrays; public class Solution {
public int[] mergeSort(int[] arr) {
if (arr.length == 1) return arr;
else {
int[] arr1 = Arrays.copyOfRange(arr, 0, arr.length/2);
int[] arr2 = Arrays.copyOfRange(arr, arr.length/2, arr.length);
return merge(mergeSort(arr1), mergeSort(arr2));
}
} public int[] merge(int[] arr1, int[] arr2) {
int len1 = arr1.length;
int len2 = arr2.length;
int[] res = new int[len1+len2];
int i = 0, j=0, cur=0;
while (i<len1 && j<len2) {
if (arr1[i] <= arr2[j]) {
res[cur++] = arr1[i++];
}
else {
res[cur++] = arr2[j++];
}
}
while (i<len1) {
res[cur++] = arr1[i++];
}
while (j<len2) {
res[cur++] = arr2[j++];
}
return res;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int[] arr = sol.mergeSort(new int[]{6,5,4,8,2,1});
System.out.println(Arrays.toString(arr));
} }
在如上算法中只需稍作修改,加上一行代码,就可以求数组的逆序对
如数组 <2,3,8,6,1> 的逆序对为:<2,1> <3,1> <8,6> <8,1> <6,1> 共5个逆序对。
暴力法是O(N^2)
mergeSort可以O(NlogN)
定义一个static variable count, 然后在12行加入
public int[] merge(int[] arr1, int[] arr2) {
int len1 = arr1.length;
int len2 = arr2.length;
int[] res = new int[len1+len2];
int i = 0, j=0, cur=0;
while (i<len1 && j<len2) {
if (arr1[i] <= arr2[j]) {
res[cur++] = arr1[i++];
}
else { // arr1[i] > arr2[j];
res[cur++] = arr2[j++];
count += arr1.length - i;
}
}
while (i<len1) {
res[cur++] = arr1[i++];
}
while (j<len2) {
res[cur++] = arr2[j++];
}
return res;
}
Inplace的mergeSort不是那么好写,我还是在merge的时候用了额外空间
package ArrayMergeSort; import java.util.Arrays; public class Solution2 { public int[] mergeSort(int[] arr) {
if (arr==null || arr.length==0) return arr;
mergeSort(arr, 0, arr.length-1);
return arr;
} public void mergeSort(int[] arr, int l, int r) {
if (r-l == 0) return;
int m = (l+r)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, m+1, r);
} public void merge(int[] arr, int l1, int r1, int l2, int r2) {
int[] temp = new int[r2-l1+1];
int i1=l1, i2=l2, cur=0;
while (i1<=r1 && i2<=r2) {
if (arr[i1] <= arr[i2]) temp[cur]=arr[i1++];
else temp[cur] = arr[i2++];
cur++;
}
while(i1<=r1) temp[cur++]=arr[i1++];
while(i2<=r2) temp[cur++]=arr[i2++];
cur = 0;
for (int i=l1; i<=r2; i++) arr[i]=temp[cur++];
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
int[] arr = sol.mergeSort(new int[]{6,5,4,2,1});
System.out.println(Arrays.toString(arr));
} }
Iterative Merge Sort大致的算法是,假设每一层需要merge许多数组 A和B数组是其中两个,i 可以理解为A或B的size,从1一直到array.length/2. j 可以理解为一组A和B之中B的结束位置。 Merge函数跟上面一样
Iterative Merge Sort
public static T[] Iterative(T[] array, IComparer<T> comparer)
{
for (int i = 1; i <= array.Length / 2 + 1; i *= 2)
{
for (int j = i; j < array.Length; j += 2 * i)
{
Merge(array, j - i, j, Math.Min(j + i, array.Length), comparer);
}
} return array;
}
Summary: Merge Sort of Array && 求逆序对的更多相关文章
- 【剑指offer】求逆序对的个数
2013-09-07 10:50:31 面试题36:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字构成一个逆序对.输入一个数组,求出这个数组中逆序对的总数. 小结: 最直观的的方法是: ...
- POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]
Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...
- POJ 4020 NEERC John's inversion 贪心+归并求逆序对
题意:给你n张卡,每张卡上有蓝色和红色的两种数字,求一种排列使得对应颜色数字之间形成的逆序对总数最小 题解:贪心,先按蓝色排序,数字相同再按红色排,那么蓝色数字的逆序总数为0,考虑交换红色的数字消除逆 ...
- 归并排序+归并排序求逆序对(例题P1908)
归并排序(merge sort) 顾名思义,这是一种排序算法,时间复杂度为O(nlogn),时间复杂度上和快排一样 归并排序是分治思想的应用,我们先将n个数不断地二分,最后得到n个长度为1的区间,显然 ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】
Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...
随机推荐
- Sign-Magnitude Representation
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION There are several alt ...
- RSA
https://en.wikipedia.org/wiki/RSA_(cryptosystem)
- ATT 汇编语法
在研华的pc104上使用看门狗要使用汇编.使用汇编来修改CMOS里面的参数.也就是内联汇编.linux下gcc只支持ATT汇编.所以这儿有必要将ATT语法学习学习.以后需要的时候翻出来温习温习. ...
- Java正常关闭资源的方式
在实际开发中,经常需要在程序中打开一些物理资源,如数据库连接.网络连接.磁盘文件等,打开这些物理资源之后必须显式关闭,否则将会引起资源泄漏. JVM的垃圾回收机制不会回收这些资源,垃圾回收机制属于Ja ...
- IE6/7中setAttribute不支持class/for/rowspan/colspan等属性
如设置class属性 ? 1 el.setAttribute('class', 'abc'); 在IE6/7中样式“abc”将没有起作用,虽然使用el.getAttribute('class')能取到 ...
- jeecg的cq查询方式
@RequestMapping(params = "datagrid") public void datagrid(TbStudentDepEntity tbStudentD ...
- LINQ延迟查询的例子
//linq延迟查询.两次查询结果不同 List<string> l = new List<string>() { "aaa", "bbb&quo ...
- 更新Delphi中SVN客户端版本的方法
Delphi从XE以后集成里SVN客户端, 安装完Delphi以后, 在bin\subversion下, 存放的就是SVN客户端文件, 可惜版本有点低(好像是1.7的) 如果想更新成高版本的客户端文件 ...
- FW nexus docker
原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy) ...
- 在脚本中操作plist文件
终端输入: /usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" /Users/achen/Desktop/testBundl ...