归并排序 求逆序数 链表的归并排序 多线程归并排序 java
import java.util.Scanner;
public class Main {
private static int count=0;
public static void mergesort(int a[],int low,int high)
{
if(low<high)
{
int mid=(low+high)>>1;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
private static void merge(int[] a, int low, int mid, int high) {
int temp[]=new int[high-low+1]; //开辟额外空间
int index=0;
int beg1=low;
int beg2=mid+1;
while(beg1<=mid&&beg2<=high) //如果两者都存在,选在较少的一个
{
if(a[beg1]<=a[beg2])
{
temp[index++]=a[beg1++];
}
else
{
temp[index++]=a[beg2++];
count+=mid-beg1+1; // 如果选中后者,对于后者会出现mid-beg1+1的逆序数
}
}
while(beg1<=mid) //剩下的元素
{
temp[index++]=a[beg1++];
}
while(beg2<=high)
{
temp[index++]=a[beg2++];
}
for(int i=0;i<index;i++)
{
a[low+i]=temp[i];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn=new Scanner(System.in);
int len=scn.nextInt();
while(len-->0)
{
count=0;
int len2=scn.nextInt();
int a[]=new int[len2];
for(int i=0;i<len2;i++)
{
a[i]=scn.nextInt();
}
mergesort(a,0,len2-1);
System.out.println(count);
}
}
}
leetcode 中的链表排序
public class Solution {
public ListNode sortList(ListNode head) {
if(head==null) return null;
if(head.next==null) return head;
ListNode list=split(head);
return merge(sortList(head),sortList(list));
}
//divide into two part ,return the middle address
private ListNode split(ListNode head) {
ListNode qucik=head;
ListNode slow=head;
ListNode pre=null;
while(qucik!=null)
{
pre=slow;
slow=slow.next;
qucik=qucik.next;
if(qucik!=null) qucik=qucik.next;
}
pre.next=null;
return slow;
}
public ListNode merge(ListNode head,ListNode middle)
{
ListNode p1=head;
ListNode p2=middle;
ListNode h=new ListNode(-1);
ListNode tail=h; //insert into tail;
while(p1!=null&&p2!=null)
{
if(p1.val<=p2.val)
{
tail.next=p1;
tail=tail.next;
p1=p1.next;
}
else
{
tail.next=p2;
tail=tail.next ;
p2=p2.next;
}
}
if(p1!=null)
{
tail.next=p1;
tail=tail.next ;
}
if(p2!=null)
{
tail.next=p2;
tail=tail.next ;
}
return h.next;
}
}
多线程 的归并排序
归并排序 求逆序数 链表的归并排序 多线程归并排序 java的更多相关文章
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
随机推荐
- yii2 model常用验证规则
//字段必填[['username'],'required','message'=>'{attribute}不能为空!'][['username','password'], 'required' ...
- hdu 5626 Clarke and points 数学推理
Clarke and points Problem Description The Manhattan Distance between point A(XA,YA) and B(XB,YB) i ...
- OFBiz进阶之HelloWorld(一)创建热部署模块
创建热部署模块 参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Developm ...
- CODEVS 2055 集合划分
[题目描述] 对于从1到N(1<=N<=39)的连续整数集合,划分成两个子集合,使得每个集合的数字之和相等. 举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,他们每个的所有数字 ...
- CSS 居中方法集锦(*******************************)
记录收集纯CSS层面实现的水平.垂直居中方法可用于块级.行内快.内联元素以及文字图片等. 水平或垂直居中 1.1 text-align1.2 margin1.3 line-height1.4 pa ...
- c++ 内存泄露的检查
对于c++的内存泄露检测,除了我们自己手动检查以外,还可以使用c++中的函数来帮助我们检测, 如下代码: #include "stdafx.h" #include <stri ...
- PHP漏洞全解(二)-命令注入攻击
本文主要介绍针对PHP网站常见的攻击方式中的命令攻击.Command Injection,即命令注入攻击,是指这样一种攻击手段,黑客通过把HTML代码输入一个输入机制(例如缺乏有效验证限制的表格域)来 ...
- Linux系统调用列表
转自Linux系统调用列表 一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtabl ...
- Docker 监控- Prometheus VS Cloud Insight
如今,越来越多的公司开始使用 Docker 了,2 / 3 的公司在尝试了 Docker 后最终使用了它.为了能够更精确的分配每个容器能使用的资源,我们想要实时获取容器运行时使用资源的情况,怎样对 D ...
- IBM
http://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1306mongodb2/