(一)归并排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiangshangchunjiezi/article/details/88102185

归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治策略

可以看出这种结构像一个完全二叉树,递归深度为log2n

 

从上往下的归并排序:它与"从下往上"在排序上是反方向的。它基本包括3步:
① 分解 -- 将当前区间一分为二,即求分裂点 mid = (low + high)/2;
② 求解 -- 递归地对两个子区间a[low...mid] 和 a[mid+1...high]进行归并排序。递归的终结条件是子区间长度为1。
③ 合并 -- 将已排序的两个子区间a[low...mid]和 a[mid+1...high]归并为一个有序的区间a[low...high]。 

public class HelloWorld {
	public static void MergeSort(int[] arr,int low,int high)
	{
		if(low<high)//能再分,就继续将数组分割
		{
			int mid=(low+high)/2;
			
			//low-mid有序,mid+1-high有序
			MergeSort(arr,low,mid);
			MergeSort(arr,mid+1,high);
			
			//将arr low-mid,mid+1-high合并成有序
			Sort(arr,low,mid,high);
		}
	}
	
	public static void Sort(int[] arr,int low,int mid,int high)
	{
		int i=low;//指向low-mid数组的起点
		int j=mid+1;//指向mid+1-high数组的起点
		int[] temp=new int[high-low+1];//为了不破坏arr数组,将数据先放入temp中
		int k=0;//k是此次要存入temp的索引
		
		//将数据从小到大存入到temp数组中
		for(;i<=mid&&j<=high;k++)
		{
			if(arr[i]<arr[j])
			{
				temp[k]=arr[i];
				i++;
			}
			else{
				temp[k]=arr[j];
				j++;
			}
		}
		
		//可能存在 第一个数组的数均大于第二个数组,或者第一个数组数据有剩余
		for(;i<=mid;i++)
		{
			temp[k]=arr[i];
			k++;
		}
		//同理,第二个数组
		for(;j<=high;j++)
		{
			temp[k]=arr[j];
			k++;
		}
		
		//将temp数据放入arr中相应位置
		for(int m=0;m<temp.length;m++)
		{
			arr[low+m]=temp[m];
		}
	}
    public static void main(String []args) {
      int[] arr=new int[]{10,8,100,101};
	  MergeSort(arr,0,arr.length-1);
	  for(int m=0;m<arr.length;m++)
		{
			System.out.println(arr[m]);
		}
    }
}

参考:https://www.javazhiyin.com/1222.html#m

         http://www.cnblogs.com/skywang12345/p/3602369.html

猜你喜欢

转载自blog.csdn.net/jiangshangchunjiezi/article/details/88102185
今日推荐