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] + " ");
            }
        }
    }
    
  • 相关阅读:
    后缀数组模板~~~
    CF 196 Div2 D&&HDU 4679 && HDU 2196
    .......
    ~~~~~
    主站点~~~~
    C++抽象,封装,继承,多态
    查缺补漏知识点(二)
    Qt的简介
    C语言指针及C++引用
    C++单例模式
  • 原文地址:https://www.cnblogs.com/sanjun/p/9972965.html
Copyright © 2011-2022 走看看