zoukankan      html  css  js  c++  java
  • 七大排序_归并排序

    归并排序:

               时间复杂度:

       空间复杂度:O(log2^n)

      原理:采用分而治之的思想

    过程图:

                

    package com.Grammer.归并排序;
    
    import java.awt.print.PrinterAbortException;
    import java.util.Arrays;
    
    public class MergeSort02 {
        public static void main(String[] args) {
            int len=10;
            int[] arr=new int[len];
            for (int i = 0; i < len - 1; i++) {
                arr[i]= (int) (Math.random()*100);
            }
            System.out.println("原始的数组"+ Arrays.toString(arr));
            //进行排序的操作
            sort(arr);
            System.out.println("完成的数组"+ Arrays.toString(arr));
        }
        private static void sort(int[] arr){
            if(arr.length==0||arr.length==1){
                return;
            }
            //创建 临时数组
            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[t++]=arr[i++];
            }
            while (j<=right){
                temp[t++]=arr[j++];
    
            }
            t=0;
            while(left<=right){
                arr[left++]=temp[t++];
            }
        }
    }
    

      

      

  • 相关阅读:
    个人总结一些常见的css问题
    工作中的js总结
    js面向对象
    js的一些特性
    js 实现改变字体大小
    将博客搬至CSDN
    最大连续子序列----DP动态规划
    捡石子---贪心算法(huffman)
    素数环问题---深度搜索遍历
    nyoj---12 喷水装置(二)--区间覆盖问题
  • 原文地址:https://www.cnblogs.com/666boyun/p/14831267.html
Copyright © 2011-2022 走看看