25、JAVA归并排序


归并排序时间复杂度:最好最坏平均都是nlogn

空间复杂度:n

稳定性:稳定的

 

归并排序的用途:

1、排序

  (速度仅次于 快速排序,但较稳定)

2、求逆序对数【这个单独出个笔试题,求逆序对数】

  具体思路是,在归并的过程中计算每个小区间的逆序对数,进而计算出大区间的逆序对数(也可以用树状数组来求解)

 

JAVA实现归并排序:

 

import java.util.Arrays;


public class MergeSort {
	public static void main(String args[]){
		int a[] = {4,4,1,6,3};
		mergeSort(a,0,a.length-1);
		/*for(int i=0;i<a.length-1;i++)
			System.out.println(a[i]);
		*/
		  System.out.println(Arrays.toString(a));
	}
	
	private static void mergeSort(int a[],int start,int end){
		//int mid = (start+end)/2;
		//int[] temp = new int[a.length];
		int mid=(start&end)+((start^end)>>1);
		if(start<end){//跳出递归,原来忘记写这一句,导致栈溢出
		mergeSort(a,start,mid);
		mergeSort(a,mid+1,end);
		merge(a,start,mid,mid+1,end);
		}
	}
	
	private static void merge(int a[],int start1,int end1,int start2,int end2){
		int i = start1;
		int j = start2;
		//int k = 0;//k与此处保持一致,System.arraycopy(temp, 0, a, start1, end2-start1+1);
		int k = start1;//k与此处保持一致,System.arraycopy(temp, start1, a, start1, end2-start1+1);
		int temp[] = new int[a.length+1];
		
		while(i<=end1 && j<=end2){
			if(a[i] < a[j]){
				temp[k++] = a[i++];
				//temp[k] = a[i];
				//i++;
			}
			else{
				temp[k++] = a[j++];
				//temp[k] = a[j];
				//j++;
			}
			//k++;
		}
		
		while(i<=end1){
			temp[k++] = a[i++];
		}
		while(j<=end2){
			temp[k++] = a[j++];
		}
		
		//k = start1;
		//for(int element:temp){//把临时数组元素赋值给原数组
			//a[k++] = element;
		System.arraycopy(temp, start1, a, start1, end2-start1+1);
		}


	

}

 

 

删除注释后的完整可运行代码:

import java.util.Arrays;

public class MergeSort {
	public static void main(String args[]){
		int a[] = {4,4,1,6,3};
		mergeSort(a,0,a.length-1);
		/*for(int i=0;i<a.length-1;i++)
			System.out.println(a[i]);
		*/
		  System.out.println(Arrays.toString(a));
	}
	
	private static void mergeSort(int a[],int start,int end){
		//int mid = (start+end)/2;
		int mid=(start&end)+((start^end)>>1);
		if(start<end){//跳出递归,原来忘记写这一句,导致栈溢出
		mergeSort(a,start,mid);
		mergeSort(a,mid+1,end);
		merge(a,start,mid,mid+1,end);
		}
	}
	
	private static void merge(int a[],int start1,int end1,int start2,int end2){
		int i = start1;
		int j = start2;
		int k = start1;//k与此处保持一致,System.arraycopy(temp, start1, a, start1, end2-start1+1);
		int temp[] = new int[a.length+1];
		
		while(i<=end1 && j<=end2){
			if(a[i] < a[j]){
				temp[k++] = a[i++];
			}
			else{
				temp[k++] = a[j++];
			}
		}
		
		while(i<=end1){
			temp[k++] = a[i++];
		}
		while(j<=end2){
			temp[k++] = a[j++];
		}
		
	
		System.arraycopy(temp, start1, a, start1, end2-start1+1);//从temp数组复制到a数组
		}
}


 


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值