zoukankan      html  css  js  c++  java
  • 归并排序 和 小和问题

     1 package com.aixuexi.contact;
     2 
     3 public class Mergesort {
     4     
     5     public static void mergeSort(int arr[]) {
     6         if(arr == null || arr.length <= 2) {
     7             return ;
     8         }
     9         mergeSort(arr,0,arr.length-1);
    10     }
    11     public static void mergeSort(int arr[],int L,int R) {
    12         
    13         if(L == R) {
    14             return ;
    15         }
    16         int mid = (L + R ) / 2;
    17         mergeSort(arr,L,mid);
    18         mergeSort(arr,mid+1,R);
    19         merge(arr,L, mid, R);
    20     }
    21     public static void merge(int arr[],int L,int mid,int R) {
    22         int arr1[] = new int[R -  L + 1];
    23         int i = 0;
    24         int p1 = L;
    25         int p2 = mid + 1;
    26         while(p1 <= mid && p2 <= R) {
    27             arr1[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
    28         }
    29         
    30         while (p1 <= mid) {
    31             arr1[i++] = arr[p1++];
    32         }
    33         while (p2 <= R) {
    34             arr1[i++] = arr[p2++];
    35         }
    36         for (i = 0; i < arr1.length; i++) {
    37             arr[L + i] = arr1[i];
    38         }
    39         
    40     }
    41     
    42     public static void main(String[] args) {
    43         int arr[] = new int[] {1,4,8,3,7,3,43,43,6,8,87,9,4,67};
    44         mergeSort(arr);
    45         for(int i = 0; i < arr.length; i++) {
    46             System.out.println(arr[i]);
    47         }
    48     }
    49 }
    package com.aixuexi.contact;
    
    /* 2019/7/16  09点25分
     * 
     * 总结:     1个问题行数   --28 行
     * 注意点     38行
     * 
     * return mergeSort(arr, l, mid) + mergeSort(arr, mid + 1, r) + merge(arr, l, mid, r);
     * res += arr[p1] < arr[p2] ? (r - p2 + 1) * arr[p1] : 0; //算下标
     */
    
    public class SmallSum {
    
        public static int smallSum(int[] arr) {
            if (arr == null || arr.length < 2) {
                return 0;
            }
            return mergeSort(arr, 0, arr.length - 1);
        }
    
        public static int mergeSort(int[] arr, int l, int r) {
            if (l == r) {
                return 0;
            }
            int mid = l + ((r - l) >> 1); 
    //        mergeSort(arr, l, mid); // 0
    //        mergeSort(arr, mid + 1, r); // 0
    //        int ret = merge(arr, l, mid, r); //ret = 9
            
            return mergeSort(arr, l, mid) + mergeSort(arr, mid + 1, r) + merge(arr, l, mid, r);
        }
    
        public static int merge(int[] arr, int l, int m, int r) {
            int[] help = new int[r - l + 1];
            int i = 0;
            int p1 = l;
            int p2 = m + 1;
            int res = 0;
            while (p1 <= m && p2 <= r) {
                res += arr[p1] < arr[p2] ? (r - p2 + 1) * arr[p1] : 0; //算下标
                help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
            }
            while (p1 <= m) {
                help[i++] = arr[p1++];
            }
            while (p2 <= r) {
                help[i++] = arr[p2++];
            }
            for (i = 0; i < help.length; i++) {
                arr[l + i] = help[i];
            }
            return res;
        }
        
        public static void main(String[] args) {
            int arr[] = new int[] {1,3,4,2,5};
            int res = smallSum(arr);
            System.out.println(res);
    //        for(int i = 0; i < arr.length; i++) {
    //            System.out.println(arr[i]);
    //        }
            
        }
    
    }
  • 相关阅读:
    linux学习方法之一
    HDU 1556 Color the ball
    Object-c学习之路十(NSNumber&NSValue)
    蜂鸣器驱动方式源程序--有源无源通用
    Wordpress更换主题之后出错
    mybatis_Generator配置
    Logistic Regression
    求两个字符串的最大公共字串
    数据结构排序系列详解之二 希尔排序
    《mysql必知必会》学习_第五章
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/11199527.html
Copyright © 2011-2022 走看看