36 数组中的逆序对

描述:


实现代码:

package question36;

public class InversePairs {
	static int count=0;
	public static int inversePairs(int [] array) {

		int res=mergeSort(array,0,array.length-1);
		
		return res;
	}
	private static int mergeSort(int[] array, int low, int high) {
		// TODO Auto-generated method stub
		int mid=((high-low)>>1)+low;
		if (high>low) {
			mergeSort(array, low, mid);
			mergeSort(array, mid+1, high);
			merge (array,low,mid,high);
		}
		return count;
	}
	private static void merge(int[] array, int low, int mid, int high) {
		// TODO Auto-generated method stub
		int temp[]=new int[high-low+1];
		int k=high-low;
		int i=mid;
		int j=high;
		while (i>=low&&j>=mid+1) {
			if (array[i]>array[j]) {
				count=count+j-mid;
				temp[k--]=array[i--];
				
			}else {
				temp[k--]=array[j--];
			}
			
		}
		
		while (i>=low) {
			temp[k--]=array[i--];
		}
		while (j>=mid+1) {
			temp[k--]=array[j--];
		}
		
		for (int m = 0; m < temp.length; m++) {
			array[low+m]=temp[m];
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
                int array[]={7,5,6,4};
                int res=inversePairs(array);
                System.out.println(res);
                System.out.println("善哉!善哉!大地众生皆具如来智慧德相,但因妄想执着而不能证得,只要恢复了自己本有的清净自性,一切众生皆可成佛!"); 
	}

}

测试结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值