数据结构--归并排序

mac2022-06-30  22

                                           归并排序

1 定义

归并排序是一种概念上最简单的排序算法,与快速排序一样,归并排序也是基于分治法的。归并排序将待排序的元素序列分成两个长度相等的子序列,为每一个子序列排序,然后再将他们合并成一个子序列。合并两个子序列的过程也就是两路归并。

2 思路以及图解

 

归并排序是采用分治法的一个非常典型的应用。归并排序的思想就是先递归分解数组,再合并数组。

将数组分解最小之后,然后合并两个有序数组,基本思路是比较两个数组的最前面的数,谁小就先取谁,取了后相应的指针就往后移一位。然后再比较,直至一个数组为空,最后把另一个数组的剩余部分复制过来即可。

3 分治法 归并排序

/** * @Author liuhaidong * @Description 进行归并 * @param * @Date 21:07 2019/10/2 0002 */ private static void MergrSort(int[] arr, int startIndex, int centerIndex, int enIndex) { int[] tempArr = new int[enIndex-startIndex+1]; //定义一个临时数组 int i = startIndex; //定义一个左边数组的起始索引 int j = centerIndex+1; //定义一个右边数组的起始索引 int index = 0; //定义一个临时数组的起始索引 while (i <= centerIndex && j<=enIndex){ if(arr[i] <= arr[j]){ tempArr[index] = arr[i]; i++; }else { tempArr[index] = arr[j]; j++; } index++; } //处理剩余元素 可能是左边元素,也可能是右边元素 while(i <= centerIndex){ tempArr[index] = arr[i]; i++; index++; } while(j <= enIndex){ tempArr[index] = arr[j]; j++; index++; } for(int k = 0;k<tempArr.length;k++){ arr[k+startIndex] = tempArr[k]; } }

 

/** * @Author liuhaidong * @Description 进行拆分 不断的从中间拆 * @param * @Date 21:19 2019/10/2 0002 */ private static void Split(int[] arr, int startIndex, int endIndex) { //计算中间索引 int centerIndex = (startIndex + endIndex)/2; if(startIndex < endIndex){ Split(arr, startIndex, centerIndex); Split(arr, centerIndex+1, endIndex); MergrSort(arr,startIndex,centerIndex,endIndex); //进行归并 } }

主函数

public static void main(String[] args) { int[] arr = {10,30,2,1,0,8,7,5,19,29}; //原始待排序数组 Split(arr,0,arr.length-1); //拆分 我们先给一个左右两边是有序的一个数组,先来进行归并排序 {4,5,7,8, 1,2,3,6} //MergrSort(arr,0,3,arr.length-1); //归并 进行此函数的前提是本身就是两半有序数组 System.out.println(Arrays.toString(arr)); //输出原数组 }

4 关键代码讲解

while (i <= centerIndex && j<=enIndex){ if(arr[i] <= arr[j]){ tempArr[index] = arr[i]; i++; }else { tempArr[index] = arr[j]; j++; } index++; }

再来看看治阶段,我们需要将两个已经有序的子序列合并成一个有序序列,比如上图中的最后一次合并,要将[4,5,7,8]和[1,2,3,6]两个已经有序的子序列,合并为最终序列[1,2,3,4,5,6,7,8],来看下实现步骤。

5 代码优化

//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间

package sortdemo; import java.util.Arrays; /** * Created by chengxiao on 2016/12/8. */ public class MergeSort { public static void main(String []args){ int []arr = {9,8,7,6,5,4,3,2,1}; sort(arr); System.out.println(Arrays.toString(arr)); } public static void sort(int []arr){ int []temp = new int[arr.length]; //在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间 sort(arr,0,arr.length-1,temp); } private static void sort(int[] arr,int left,int right,int []temp){ if(left<right){ int mid = (left+right)/2; sort(arr,left,mid,temp);//左边归并排序,使得左子序列有序 sort(arr,mid+1,right,temp);//右边归并排序,使得右子序列有序 merge(arr,left,mid,right,temp);//将两个有序子数组合并操作 } } private static void merge(int[] arr,int left,int mid,int right,int[] temp){ int i = left;//左序列指针 int j = mid+1;//右序列指针 int t = 0;//临时数组指针 while (i<=mid && j<=right){ if(arr[i]<=arr[j]){ temp[t++] = arr[i++]; }else { temp[t++] = arr[j++]; } } while(i<=mid){//将左边剩余元素填充进temp中 temp[t++] = arr[i++]; } while(j<=right){//将右序列剩余元素填充进temp中 temp[t++] = arr[j++]; } t = 0; //将temp中的元素全部拷贝到原数组中 while(left <= right){ arr[left++] = temp[t++]; } } }

6 时间复杂度

归并排序是稳定排序,它也是一种十分高效的排序,能利用完全二叉树特性的排序一般性能都不会太差。java中Arrays.sort()采用了一种名为TimSort的排序算法,就是归并排序的优化版本。从上文的图中可看出,每次合并操作的平均时间复杂度为O(n),而完全二叉树的深度为|log2n|。总的平均时间复杂度为O(nlogn)。而且,归并排序的最好,最坏,平均时间复杂度均为O(nlogn)。

最新回复(0)