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. rsync与inotify 数据同步

    发布:thebaby   来源:脚本学堂     [大 中 小] 本文介绍下,在linux系统中,使用rsync与inotify实现数据同步的一个实例,有研究文件同步的朋友可以作个参考.本文转自:ht ...

  2. The preview is empty because of the setting.Check the generation option.

    前些日子在pd中添加存储过程, 参考:深蓝居的博文 http://www.cnblogs.com/studyzy/archive/2009/12/18/1627334.html 创建视图的时候,会在属 ...

  3. 006.Compile方法

    Delphi procedure Compile; 类型:procedure 可见性:public 所在单元:System.RegularExpressionsCore 父类:TPerlRegEx 此 ...

  4. 《C和指针》读书笔记——第三章 数据

    1.typedef:为各种数据类型定义新名字 typedef char  *ptr_to_char; ptr_to_char a;//声明a是一个指向字符的指针. 2.链接属性:extern;stat ...

  5. 笨方法学python 33课

    今天Eiffel看到了第33章,任务是把一个while循环改成一个函数. 我在把while循环改成函数上很顺利,但是不知道怎么写python的主函数,在参数的调用上也出现了问题. 通过查资料,发现py ...

  6. about Q&A in installing linux[centos6,7]

    keywords:grub1,grub2,gnome,kde, question describe:install centos7 by U disk,出现问题, 解决办法: install cent ...

  7. WPF学习笔记2——XAML之2

    三.事件处理程序与代码隐藏 例如,为一个Page添加一个Button控件,并为该Button添加事件名称Button_Click: <Page xmlns="http://schema ...

  8. js日期范围初始化,得到前一个月的日期

    今天做时间范围的初始化设定,开始时间是当前时间的前一个月,终于找到完美的解决方案了. Date.prototype.format = function(format) {     var o = { ...

  9. RedHat和CentOS使用本地yum源配置

    2013-04-01 11:38:30 标签:本地yum源 版权声明:原创作品,谢绝转载!否则将追究法律责任. 使用yum命令安装所需的软件,如果设备网络状况很好,当然也没必要去配置本地yum源,直接 ...

  10. Java学习--封装、继承、多态

    接下来几天会根据http://www.cnblogs.com/chenssy/category/525010.html中讲解的java内容做个学习笔记,在此感谢一下这位大仙!! 一.封装 对于封装而言 ...