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

        /**
         * 分+合方法
         *
         * @param arr
         * @param left
         * @param right
         * @param temp
         */
        public static void mergeSort(int[] arr, int left, int right, int[] temp) {
            if (left < right) {
                //中间索引
                int mid = (left + right) / 2;
                //向左递归进行分解
                mergeSort(arr, left, mid, temp);
                //向右递归进行分解
                mergeSort(arr, mid + 1, right, temp);
                //合并
                merge(arr, left, mid, right, temp);
    
            }
        }
    
        /**
         * 合并的方法
         *
         * @param arr   排序的原始数组
         * @param left  左边有序序列的初始索引
         * @param mid   中间索引
         * @param right 右边索引
         * @param temp  做中转的数组
         */
        public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
    
            // 初始化i, 左边有序序列的初始索引
            int i = left;
            //初始化j, 右边有序序列的初始索引
            int j = mid + 1;
            // 指向temp数组的当前索引
            int t = 0;
    
            /**
             * 先把左右两边(有序)的数据按照规则填充到temp数组,直到左右两边的有序序列,有一边处理完毕为止
             */
            while (i <= mid && j <= right) {
                //如果左边的有序序列的当前元素,小于等于右边有序序列的当前元素
                //即将左边的当前元素,填充到 temp数组
                //然后 t++, i++
                if (arr[i] <= arr[j]) {
                    temp[t] = arr[i];
                    t += 1;
                    i += 1;
                } else {
                    //反之,将右边有序序列的当前元素,填充到temp数组
                    temp[t] = arr[j];
                    t += 1;
                    j += 1;
                }
            }
    
            /**
             * 把有剩余数据的一边的数据依次全部填充到temp
             */
            while (i <= mid) {
                //左边的有序序列还有剩余的元素,就全部填充到temp
                temp[t] = arr[i];
                t += 1;
                i += 1;
            }
    
            while (j <= right) {
                //右边的有序序列还有剩余的元素,就全部填充到temp
                temp[t] = arr[j];
                t += 1;
                j += 1;
            }
    
            /**
             * 将temp数组的元素拷贝到arr,注意,并不是每次都拷贝所有
             */
            t = 0;
            int tempLeft = left;
            //第一次合并 tempLeft = 0 , right = 1 //  tempLeft = 2  right = 3 // tL=0 ri=3
            //最后一次 tempLeft = 0  right = 7
            while (tempLeft <= right) {
                arr[tempLeft] = temp[t];
                t += 1;
                tempLeft += 1;
            }
    
        }
    
  • 相关阅读:
    MongoDB分片集群还原
    集群因子(Clustering Factor)
    Sunisoft.IrisSkin.SkinEngine 设置winform皮肤
    17monipdb根据IP获得区域
    ArraySegmentSample
    RichTextBox指定全部文字显示不同颜色及部分文字高亮颜色显示
    Git 基本分支规范
    C# 获取方法内参数名称
    (转)C#中的Predicate<T>与Func<T, bool>
    EF 多线程TransactionScope事务异常"事务EFTransaction类定义:与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。"
  • 原文地址:https://www.cnblogs.com/ding-dang/p/13391147.html
Copyright © 2011-2022 走看看