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

你可能感兴趣的文章
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>