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

    public class MergeSortDemo {
    
        public static void merge(int[] a, int low, int mid, int high) {
            //建立一个临时数组,大小为归并后大小
            int[] temp = new int[high - low + 1];
            //左边
            int i = low;
            //右边
            int j = mid + 1;
            //新数组的起始下标
            int k = 0;
            //把较小的数先移到新数组
            while (i <= mid && j <= high) {
                if (a[i] < a[j]) {
                    temp[k++] = a[i++];
                } else {
                    temp[k++] = a[j++];
                }
            }
            //左边剩余
            while (i <= mid) {
                temp[k++] = a[i++];
            }
            //右边剩余
            while (j <= high) {
                temp[k++] = a[j++];
            }
            //把新数组复制到原数组
            for (int index = 0; index < temp.length; index++) {
                //从分点开始
                a[index + low] = temp[index];
            }
    
        }
    
        public static void mergeSort(int[] a, int low, int high) {
            //取中点
            int mid = (low + high) / 2;
            if (low < high) {
                //左边
                mergeSort(a, low, mid);
                //右边
                mergeSort(a, mid + 1, high);
                //归并
                merge(a, low, mid, high);
            }
        }
    
        public static void main(String[] args) {
            int[] a = {21, 25, 14, 8, 3, 63, 77, 44, 66};
            mergeSort(a, 0, a.length - 1);
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
        }
    }
    
  • 相关阅读:
    最近邻插值
    tp类型自动转换和自动完成
    tp读取器和写入器
    tp模型和数据库操作方法
    tp数据库操作
    tp请求和响应
    tp配置+路由+基本操作
    git的常见操作方法
    php 检查该数组有重复值
    公众号的TOKEN配置PHP代码
  • 原文地址:https://www.cnblogs.com/sanjun/p/9972965.html
Copyright © 2011-2022 走看看