MergeSort


/**
归并排序
*/
class Solution{
	// static int count = 0;
	public static void main(String[] args) {
		int[] nums = new int[]{7,3,2,6,0,1,5,4};
		merge(nums,0,nums.length - 1);
		// System.out.println(count);
		for(int i = 0;i<nums.length;i++) System.out.print(nums[i] + " ");
		System.out.println();
	}
	public static void merge(int[] nums,int left,int right){
		int mid = left + (right - left)/2;
		if(left < right){
			merge(nums,left,mid);
			merge(nums,mid+1,right);
			mergeSort(nums,left,mid,right);
		}
	}
	public static void mergeSort(int[] nums,int left,int mid,int right){
		int[] tmp = new int[right - left + 1];
		int index = 0;
		int t1 = left;
		int t2 = mid+1;
		while(t1<=mid && t2<=right){
			if(nums[t1] <= nums[t2]){
				tmp[index] = nums[t1];
				index++;
				t1++;
			}else{
				tmp[index] = nums[t2];
				index++;
				t2++;
				// count += mid-t1+1;
			}
		}
		while(t1<=mid){
			tmp[index] = nums[t1];
			t1++;
			index++;
		}
		while(t2<=right){
			tmp[index] = nums[t2];
			t2++;
			index++;
		}
		for(int i=0;i<tmp.length;i++){
			nums[left+i] = tmp[i];
		}
	}
}
/**
利用归并排序求逆序对的数目1
*/
class Solution{
	static int count = 0;
	public static void main(String[] args) {
		int[] nums = new int[]{7,3,2,6,0,1,5,4};
		merge(nums,0,nums.length - 1);
		System.out.println(count);
	}
	public static void merge(int[] nums,int left,int right){
		int mid = left + (right - left)/2;
		if(left < right){
			merge(nums,left,mid);
			merge(nums,mid+1,right);
			mergeSort(nums,left,mid,right);
		}
	}
	public static void mergeSort(int[] nums,int left,int mid,int right){
		int[] tmp = new int[right - left + 1];
		int index = 0;
		int t1 = left;
		int t2 = mid+1;
		while(t1<=mid && t2<=right){
			if(nums[t1] <= nums[t2]){
				tmp[index] = nums[t1];
				index++;
				t1++;
			}else{
				tmp[index] = nums[t2];
				index++;
				t2++;
				count += mid-t1+1;
			}
		}
		while(t1<=mid){
			tmp[index] = nums[t1];
			t1++;
			index++;
		}
		while(t2<=right){
			tmp[index] = nums[t2];
			t2++;
			index++;
		}
		for(int i=0;i<tmp.length;i++){
			nums[left+i] = tmp[i];
		}
	}
}
/**
利用归并排序求逆序对的数目1
*/
class Solution{
	static int[] tmp;
	static int[] nums;
	public static void main(String[] args) {
		nums = new int[]{7,3,2,6,0,1,5,4};
		tmp = new int[nums.length];
		int ans = mergeSort(0,nums.length - 1);
		System.out.println(ans);
		for(int i = 0;i<nums.length;i++) System.out.print(nums[i] + " ");
		System.out.println();

	}

	public static int mergeSort(int l,int r){
		if(l>=r) return 0;
		int m = l + (r-l)/2;
		int res = mergeSort(l,m) + mergeSort(m+1,r);
		int i = l;
		int j = m+1;
		for(int k = l;k<=r;k++){
			tmp[k] = nums[k];
		}
		for(int k = l;k<=r;k++){
			if(i == m + 1){
				nums[k] = tmp[j];
				j++;
			}
			else if(j == r + 1 || tmp[i] <= tmp[j]){
				nums[k] = tmp[i];
				i++;
			}
			else{
				res+=m-i+1;
				nums[k] = tmp[j];
				j++;
			}
		}
		for(int k = l;k<=r;k++) System.out.print(tmp[k] + " ");
		System.out.println();
		return res;

	}
}
posted @ 2021-04-26 11:45  BOTAK  阅读(30)  评论(0编辑  收藏  举报