归并排序 求逆序数 链表的归并排序 多线程归并排序 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 ...
随机推荐
- wamp——利用phpmyadmin修改数据库密码
一.wamp版本 我采用的是 WampServer2.2a-x32.exe 这个版本的安装程序. 二.问题描述 安装wamp后,mysql数据库默认密码为空,但是由于某 ...
- Stop a hung service 关闭一个无响应的windows 服务
If you ever have trouble with a service being stuck in a 'starting' or 'stopping' state, you can run ...
- PL/SQL — 变长数组
PL/SQL变长数组是PL/SQL集合数据类型中的一种,其使用方法与PL/SQL嵌套表大同小异,唯一的区别则是变长数组的元素的最大个数是有限制的.也即是说变长数组的下标固定下限等于1,上限可以扩展.下 ...
- linux系统管理
系统 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo ...
- JQ+AJAX实现多级联动
利用JQ与AJAX实现三级联动实现的效果: 当前两级改变时,后边一级或两级都会改变: 使用的数据库: html代码: <!doctype html> <html lang=" ...
- 基于strpos()函数的判断用户浏览器方法
$_SERVER['HTTP_USER_AGENT'],超全局变量,用来读取客户用的什么浏览器及其版本. strpos(),指定一个字符并搜索是否包含该字符. <html> <hea ...
- css清除浮动的几种方法整理
四种清除浮动方法如下: 1.使用空标签清除浮动.空标签可以是div标签,也可以是P 标签.这种方式是在需要清除浮动的父级元素内部的所有浮动元素后添加这样一个标签 清除浮动,并为其定义CSS代码:cle ...
- javascript 冒泡
http://www.cnblogs.com/hh54188/archive/2012/02/08/2343357.html http://blog.csdn.net/xuefeng0707/arti ...
- vs2012+cmake+opencv+opencv unable to find a build program corresponding to "Visual Studio 12 Win64". CMAKE_MAKE_PROGRAM is not set
搜索了下,说什么的都有! 一,提示找不到 cmake-2.8.12.1 的 modles 卸载了cmake后发现 cmd 中的 cmake --version 还是 2.8.11.1 找到是我的cyg ...
- MySql从服务器延迟解决方案
在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下:Master_Log_File: SLAVE中的I/ ...