• 基础算法4——归并排序


    View Code
     1 import java.util.Random;
     2 
     3 public class MergeSort {    
     4     private double[] bridge;//辅助数组
     5     
     6     public void sort(double[] obj){
     7         if (obj == null){
     8             throw new NullPointerException("The param can not be null!");
     9         }
    10         bridge = new double[obj.length]; // 初始化中间数组
    11         mergeSort(obj, 0, obj.length - 1); // 归并排序
    12         bridge = null;
    13     }
    14     
    15     private void mergeSort(double[] obj, int left, int right){
    16         if (left < right){
    17             int center = (left + right) / 2;
    18             mergeSort(obj, left, center);
    19             mergeSort(obj, center + 1, right);
    20             merge(obj, left, center, right);
    21         }
    22     }
    23     
    24     private void merge(double[] obj, int left, 
    25 int center, int right){
    26         int mid = center + 1;
    27         int third = left;
    28         int tmp = left;
    29         while (left <= center && mid <= right){ 
    30             // 从两个数组中取出小的放入中间数组
    31             if (obj[left]-obj[mid]<=10e-6){
    32                 bridge[third++] = obj[left++];
    33             } else{
    34                 bridge[third++] = obj[mid++];
    35             }
    36         }
    37 
    38         // 剩余部分依次置入中间数组
    39         while (mid <= right){
    40             bridge[third++] = obj[mid++];
    41         }
    42         while (left <= center){
    43             bridge[third++] = obj[left++];
    44         }
    45         // 将中间数组的内容拷贝回原数组
    46         copy(obj, tmp, right);
    47     }
    48     
    49     private void copy(double[] obj, int left, int right)
    50     {
    51         while (left <= right){
    52             obj[left] = bridge[left];
    53             left++;
    54         }
    55     }
    56     
    57     public static void main(String[] args) {
    58         Random random = new Random(6);
    59 
    60         int arraysize = 10;
    61         double[] sorted = new double[arraysize];
    62         System.out.print("Before Sort:");
    63         for (int j = 0; j < arraysize; j++) {
    64             sorted[j] = (int) (random.nextDouble() * 100);
    65             System.out.print((int) sorted[j] + " ");
    66         }
    67         System.out.println();
    68 
    69         MergeSort sorter = new MergeSort();
    70         sorter.sort(sorted);
    71         
    72         System.out.print("After Sort:");
    73         for (int j = 0; j < sorted.length; j++) {
    74             System.out.print((int) sorted[j] + " ");
    75         }
    76         System.out.println();
    77     }
    78 
    79 }
  • 相关阅读:
    表单元素input与文字对齐
    css hack较全
    闭合浮动
    字体
    css中z-index属性(包含各浏览器对此兼容问题)
    ie中使用滤镜实现css3的效果
    css实现提示框
    css3实现渐变效果
    css伪类
    .net core运用application/x-www-form-urlencoded发起post请求
  • 原文地址:https://www.cnblogs.com/perfy/p/3069198.html
Copyright © 2020-2023  润新知