博客
关于我
归并排序
阅读量:525 次
发布时间:2019-03-08

本文共 2172 字,大约阅读时间需要 7 分钟。

归并排序是一个高效的排序算法,通过分而治之的思想将问题分解成更小的子问题。归并排序的时间复杂度为O(n log n),空间复杂度为O(n),并且算法本身是稳定的。

以下是归并排序的递归实现:

public class mergeTest {    public static void mergeSort(int[] array) {        mergeSort(array, 0, array.length - 1);    }    private static void mergeSort(int[] array, int low, int high) {        if (low >= high) {            return;        }        int mid = (low + high) / 2;        mergeSort(array, low, mid);        mergeSort(array, mid + 1, high);        merge(array, low, mid, high);    }    public static void merge(int[] array, int low, int mid, int high) {        int s1 = low;        int s2 = mid + 1;        int[] tempArray = new int[high - low + 1];        int i = 0;        while (s1 <= mid && s2 <= high) {            if (array[s1] <= array[s2]) {                tempArray[i++] = array[s1++];            } else {                tempArray[i++] = array[s2++];            }        }        while (s1 <= mid) {            tempArray[i++] = array[s1++];        }        while (s2 <= high) {            tempArray[i++] = array[s2++];        }        for (int j = 0; j < tempArray.length; j++) {            array[low + j] = tempArray[j];        }    }}

以下是归并排序的非递归实现:

public class mergeTest1 {    public static void mergeSort(int[] array) {        for (int i = 1; i < array.length; i *= 2) {            merge(array, i);        }    }    public static void merge(int[] array, int gap) {        int[] tempArray = new int[array.length];        int k = 0;        int s1 = 0;        int e1 = s1 + gap - 1;        int s2 = e1 + 1;        int e2 = s2 + gap - 1;        while (s1 < e1 && s2 < e2) {            if (array[s1] <= array[s2]) {                tempArray[k++] = array[s1++];            } else {                tempArray[k++] = array[s2++];            }        }        while (s1 < e1) {            tempArray[k++] = array[s1++];        }        while (s2 < e2) {            tempArray[k++] = array[s2++];        }        for (int i = 0; i < array.length; i++) {            array[i] = tempArray[i];        }    }}

这两个实现都采用了归并排序的思想,通过合并有序的小段来得到最终的有序数组。递归实现的代码更为直观,但需要注意递归深度的问题。大多数编程语言都对递归深度有限制,超过限制会导致栈溢出。因此在实际应用中,需要小心合理选择排序的输入规模。非递归实现避免了这个问题,但代码结构稍微复杂一些,需要显式地管理多段数组的合并过程。

转载地址:http://tbrnz.baihongyu.com/

你可能感兴趣的文章
OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
查看>>
OAuth2:项目演示-模拟微信授权登录京东
查看>>
OA系统多少钱?OA办公系统中的价格选型
查看>>
OA系统选型:选择好的工作流引擎
查看>>
OA让企业业务流程管理科学有“据”
查看>>
OA项目之我的会议(会议排座&送审)
查看>>
OA项目之我的会议(查询)
查看>>
Object c将一个double值转换为时间格式
查看>>
object detection之Win10配置
查看>>
object detection训练自己数据
查看>>
object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
查看>>
object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
查看>>
object detection错误之no module named nets
查看>>
Object of type 'ndarray' is not JSON serializable
查看>>
Object Oriented Programming in JavaScript
查看>>
object references an unsaved transient instance - save the transient instance before flushing
查看>>
Object.keys()的详解和用法
查看>>
OBJECTIVE C (XCODE) 绘图功能简介(转载)
查看>>
Objective-C ---JSON 解析 和 KVC
查看>>
Objective-C 编码规范
查看>>