• Java基础知识强化55:经典排序之归并排序(MergeSort)


    1. 归并排序的原理:

    原理,把原始数组分成若干子数组,对每一个子数组进行排序,

    继续把子数组与子数组合并,合并后仍然有序,直到全部合并完,形成有序的数组

    举例:

    无序数组[6 2 4 1 5 9]

      先看一下每个步骤下的状态,完了再看合并细节

    第一步: [6 2 4 1 5 9]原始状态

    第二步: [2 6] [1 4] [5 9]两两合并排序,排序细节后边介绍

    第三步: [1 2 4 6] [5 9]继续两组两组合并

    第四步: [1 2 4 5 6 9]合并完毕,排序完毕

           输出结果:[1 2 4 5 6 9]

    2. 归并排序代码实现:

     1 package com.himi.classisort;
     2 
     3 /** 
     4  * 归并排序 
     5  * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。
     6  * 然后再把有序子序列合并为整体有序序列 
     7  * 时间复杂度为O(nlogn) 
     8  * 稳定排序方式 
     9  * @param nums 待排序数组 
    10  * @return 输出有序数组 
    11  */  
    12 public class MergeSoreDemo {
    13     public static void main(String[] args) {
    14         int[] array = new int[] { 12, 33, 4, 15, 25, 55, 18 };
    15         System.out.println("排序前的数组:");
    16         printArray(array);
    17         sort(array, 0, array.length-1);
    18         
    19         System.out.println("");
    20         System.out.println("排序后的数组:");
    21         printArray(array);
    22 
    23     }
    24 
    25     public static int[] sort(int[] nums, int low, int high) {  
    26         int mid = (low + high) / 2;  
    27         if (low < high) {  
    28             // 左边  
    29             sort(nums, low, mid);  
    30             // 右边  
    31             sort(nums, mid + 1, high);  
    32             // 左右归并  
    33             merge(nums, low, mid, high);  
    34         }  
    35         return nums;  
    36     }  
    37   
    38     public static void merge(int[] nums, int low, int mid, int high) {  
    39         int[] temp = new int[high - low + 1]; 
    40         int i = low;// 左索引 
    41         int j = mid + 1;// 右索引  
    42         int k = 0;  
    43   
    44         // 把较小的数先移到新数组中  
    45         while (i <= mid && j <= high) {  
    46             if (nums[i] < nums[j]) {  
    47                 temp[k++] = nums[i++];  
    48             } else {  
    49                 temp[k++] = nums[j++];  
    50             }  
    51         }  
    52   
    53         // 把左边剩余的数移入数组  
    54         while (i <= mid) {  
    55             temp[k++] = nums[i++];  
    56         }  
    57   
    58         // 把右边边剩余的数移入数组  
    59         while (j <= high) {  
    60             temp[k++] = nums[j++];  
    61         }  
    62   
    63         // 把新数组中的数覆盖nums数组  
    64         for (int k2 = 0; k2 < temp.length; k2++) {  
    65             nums[k2 + low] = temp[k2];  
    66         }  
    67     }  
    68 
    69     public static void printArray(int[] array) {
    70         System.out.print("[");
    71         int i;
    72         for (i = 0; i < array.length; i++) {
    73 
    74             if (i == array.length - 1) {
    75                 System.out.print(array[i]);
    76             } else {
    77                 System.out.print(array[i] + ",");
    78             }
    79         }
    80         System.out.print("]");
    81     }
    82 }

    程序运行结果如下:

  • 相关阅读:
    ip、ifconfig 和 route命令
    子网掩码、前缀长度、IP地址数的换算
    ubuntu安装pacman
    linux动态链接库的生成与使用
    给nimble 设置代理 解决中国国内nimble安装nim库的问题
    区块链原理汇总
    信息系统架构与国产替代
    docker中安装vim
    IDEA拖不动文件
    文件上传
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4834419.html
Copyright © 2020-2023  润新知