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. Angular ng-repeat

    <tr ng-repeat="(key,item) in tableData"> <td class="check hidden-xs"> ...

  2. 温故知新——json

    Json简介 Json(JavaScript Object Notation)是一种轻量级的数据交换格式.它是基于javascript(Standard ECMA-262 3rd Edition - ...

  3. 【spring配置】 一组配置文件引出的问题

    applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  4. Convert.ToString和ToString的区别

    Convert.ToString能处理字符串为null的情况,不抛出异常. ToString方法不能处理字符串为null的情况,会抛出异常.如:“未将对象引用设置到对象的实例”.

  5. 常用后台frame框架

    一般后台框架结构: top:左边显示logo,右边显示模块信息. left:对应模块的菜单信息. content:具体的内容. bottom:版权.时间等一些碎屑信息. Html代码: <htm ...

  6. 纯JavaScript实现一些小功能

    题目链接:http://wenku.baidu.com/link?url=7Gbarr5q9X6h1QFRVAsHmfPp1xXagG209mvrJqBogseb4WLeRqbVKwxQieoh8SL ...

  7. Spring3事务管理——使用@Transactional 注解(转)

    文章地址:http://my.oschina.net/guanzhenxing/blog/214228

  8. [转载]MongoDB的真正性能

    最近开始研究MySQL和MongoDB,发现这方面资料不多.尤其是真正的说到点子上的文章,太少了. 有一些对比测试的文章基本上都是瞎测,测试方法都测到了马腿上,得出的结论基本上都是NoSQL毫无价值 ...

  9. sublime text 2 前端编码神器-快捷键与使用技巧介绍

    介绍网址:http://www.xuanfengge.com/sublime-text-2-artifact.html

  10. underscore vs jquery

    http://wordpress.tv/2013/06/23/k-adam-white-underscore-and-backbone-jquerys-new-friends/ http://www. ...