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

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

import java.util.Arrays;//归并排序 递归版:分而治之//时间复杂度:n*log2n//空间复杂度:O(n)//稳定性:稳定的public class mergeTest {    public static void mergeSort(int[] array){        mergeSortInternal(array,0,array.length-1);    }    //递归    private static void mergeSortInternal(int[] array, int low, int high){        if(low>=high){            return;        }        int mid=(low+high)/2;        //递归左边        mergeSortInternal(array,low,mid);        //递归右边        mergeSortInternal(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;        //申请一个新的数组,长度为high-low+1        int[] tempArray=new int[high-low+1];        //tempArray的数组下标        int i=0;        //1、当两个归并段都有数据        while(s1<=mid && s2<=high){            if(array[s1]<=array[s2]){                tempArray[i++]=array[s1++];            }else{                tempArray[i++]=array[s2++];            }        }        //2、有一个归并段已经走完        while (s1<=mid){            tempArray[i++]=array[s1++];        }        while (s2<=high){            tempArray[i++]=array[s2++];        }        //将tempArray的有序数据放回原来数组里面        for (int j = 0; j 
在这import java.util.Arrays;//归并排序  非递归版本public class mergeTest1 {    public static void mergeSort(int[] array) {        for (int i = 1; i < array.length; i *= 2) {            merge(array,i);        }    }    //gap代表每个归并段的数据    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

在这里插入图片描述

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

你可能感兴趣的文章
nio 中channel和buffer的基本使用
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>