码迷,mamicode.com
首页 > 编程语言 > 详细

求两个数组的中位数

时间:2015-04-01 00:17:53      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

package kpp.base;
/**
 * 求两个有序数组的中位数
 * 此代码适用于两个数组长度不等的情况
 * @author kpp
 *
 */
public class TwoArrayMedian {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        float a[] = {1,2,3,4};
        //float b[] = {2,3,4,5,8,9,10};
        float b[] = {8,9,10,11};
        System.out.println("中位数"+getTwoArrayMedian(a,b));
    }
    private static float getTwoArrayMedian(float a[],float b[]){
        int aLen = a.length;
        int bLen = b.length;
        float shorterArray[] = aLen <= bLen? a:b;
        float longerArray[] = aLen > bLen? a:b;
        
        System.out.println("shorterArray");
        for(int i = 0;i < shorterArray.length;i++){
            System.out.print(shorterArray[i]+" ");
        }
        System.out.println();
        
        System.out.println("longerArray");
        for(int i = 0;i < longerArray.length;i++){
            System.out.print(longerArray[i]+" ");
        }
        System.out.println();
        
        while(shorterArray.length > 1 && longerArray.length > 1){
            if(getArrayMedian(shorterArray) > getArrayMedian(longerArray)){
                float temp1[] = new float[shorterArray.length/2+1];
                System.arraycopy(shorterArray, 0, temp1, 0, shorterArray.length/2+1);
                
                System.out.println("temp1");
                for(int i = 0;i < temp1.length;i++){
                    System.out.print(temp1[i]+" ");
                }
                System.out.println();
                
                int delCount = shorterArray.length-shorterArray.length/2-1;
                float temp2[] = new float[longerArray.length-delCount];
                System.arraycopy(longerArray, delCount-1, temp2, 0,longerArray.length-delCount);
                
                System.out.println("temp2");
                for(int i = 0;i < temp2.length;i++){
                    System.out.print(temp2[i]+" ");
                }
                System.out.println();
                
                shorterArray = temp1;
                longerArray = temp2;
            }else if(getArrayMedian(shorterArray) < getArrayMedian(longerArray)){
                float temp1[] = new float[shorterArray.length-shorterArray.length/2];
                System.arraycopy(shorterArray, shorterArray.length/2, temp1, 0, shorterArray.length-shorterArray.length/2);
                
                System.out.println("temp1");
                for(int i = 0;i < temp1.length;i++){
                    System.out.print(temp1[i]+" ");
                }
                System.out.println();
                
                int delCount = shorterArray.length/2;
                float temp2[] = new float[longerArray.length-delCount];
                System.arraycopy(longerArray, 0, temp2, 0,longerArray.length-delCount);
                
                System.out.println("temp2");
                for(int i = 0;i < temp2.length;i++){
                    System.out.print(temp2[i]+" ");
                }
                System.out.println();
                
                shorterArray = temp1;
                longerArray = temp2;
            }else{
                return getArrayMedian(shorterArray);
            }
            
        }
        
        //将剩余一个元素的数组插入到另一个数组中
        if(longerArray.length > 1){
            float key = shorterArray[0];
            float rsArray[] = new float[longerArray.length+1];
            System.arraycopy(longerArray, 0, rsArray, 0,longerArray.length);
            rsArray[rsArray.length-1] = key;
            
            System.out.println("key"+key);
            
            System.out.println("rsArray");
            for(int i = 0;i < rsArray.length;i++){
                System.out.print(rsArray[i]+" ");
            }
            System.out.println();
            
            if(key < rsArray[rsArray.length-2]){
                float temp = key;
                
                int j = 0;
                for( j = rsArray.length-2;j >=0&&temp < rsArray[j];j--);
                //统一向右移动
                for(int k = rsArray.length-2;k >= j+1;k--){
                    rsArray[k+1]=rsArray[k];
                }
                //插入正确位置
                rsArray[j+1] = temp;
            }
            
            System.out.println("排序后的rsArray");
            for(int i = 0;i < rsArray.length;i++){
                System.out.print(rsArray[i]+" ");
            }
            System.out.println();
            
            return getArrayMedian(rsArray);
        }else{
            return (shorterArray[0]+longerArray[0])/2;
        }
        
        
    }
    /**
     * 求一个数组的中位数
     * @param data
     * @return
     */
    private static float getArrayMedian(float data[]){
        int len = data.length;
        int mid = len/2;
        if(len%2 == 0){
            System.out.println("临时中位数"+(data[mid]+data[mid-1])/2);
            return (data[mid]+data[mid-1])/2;
        }else{
            return data[mid];
        }
    }
    

}

 

求两个数组的中位数

标签:

原文地址:http://www.cnblogs.com/kangpp/p/4382319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!