博客
关于我
归并排序
阅读量: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/

你可能感兴趣的文章
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx反向代理与正向代理配置
查看>>
Nginx反向代理是什么意思?如何配置Nginx反向代理?
查看>>
nginx反向代理解决跨域问题,使本地调试更方便
查看>>
nginx启动脚本
查看>>
Nginx在Windows下载安装启动与配置前后端请求代理
查看>>
Nginx多域名,多证书,多服务配置,实用版
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>