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的更多相关文章

  1. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  2. [CF 351B]Jeff and Furik[归并排序求逆序数]

    题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...

  3. POJ2299 Ultra-QuickSort(归并排序求逆序数)

    归并排序求逆序数   Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descri ...

  4. POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 ...

  5. HDU 3743 Frosh Week(归并排序求逆序数)

    归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...

  6. hiho一下 第三十九周 归并排序求逆序数

    题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...

  7. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  8. poj2299解题报告(归并排序求逆序数)

    POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...

  9. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

随机推荐

  1. Sublime Text 3 代码格式化插件推荐 CodeFormatter

    CodeFormatter CodeFormatter has support for the following languages: * PHP - By PHP_Beautifier* Java ...

  2. Linux-ubuntu

    在VMware安装ubuntu时磁盘分配如下: /boot 100MB / 2G swap 512MB /home 5G /var 1G /usr 5G+. 设置root权限:sudo passwd ...

  3. Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)

    MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...

  4. org.springframework.web.servlet.view.InternalResourceViewResolver

    http://blog.csdn.net/superdog007/article/details/28857495 我们在controller里面经常这样return一个ModelAndView: r ...

  5. 一步步学习NHibernate(3)——NHibernate增删改查

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们配置了以下NHibernate的运行环境, 并介绍了NHibernate的中两个非常中重要的接口"I ...

  6. 更改Keil工程名

    假设原工程名为A,需要改成B. 1, 在工程目录下,把A.vuopt和A.uvproj改成B.uvopt和B.uvproj. 2,删除其他A文件. 3,打开工程B.然后修改下面位置: Project ...

  7. quicksort+binarySearch

    描述 数轴上有n个点,对于任一闭区间 [a, b],试计算落在其内的点数. 输入 第一行包括两个整数:点的总数n,查询的次数m. 第二行包含n个数,为各个点的坐标. 以下m行,各包含两个整数:查询区间 ...

  8. 2014年50个程序员最适用的免费JQuery插件

    有用的jQuery库是设计师和开发者之间一个非常熟悉的短语.这是现在互联网中最流行的JavaScript函数库之一.每个设计师和开发人员都应该知道它的重要性,而且熟悉它的功能和特点. jQuery几乎 ...

  9. 【深度学习系列3】 Mariana CNN并行框架与图像识别

    [深度学习系列3] Mariana CNN并行框架与图像识别 本文是腾讯深度学习系列文章的第三篇,聚焦于腾讯深度学习平台Mariana中深度卷积神经网络Deep CNNs的多GPU模型并行和数据并行框 ...

  10. sql replace into 与 insert into

    sql replace into用法详细说明 REPLACE的运行与INSERT很相似.只有一点例外,假如表中的一个旧记录与一个用于PRIMARY KEY或一个UNIQUE索引的新记录具有相同的值,则 ...