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

算法 - 两个有序数组合并成一个有序数组

时间:2017-05-04 13:29:42      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:als   length   false   nbsp   turn   bsp   public   list   i++   

    //两个有序数组的合并函数
    public static int[] MergeList(int a[],int b[])
    {
        int result[];  
        if(checkSort(a) && checkSort(b))  //检查传入的数组是否是有序的
        {
            result = new int[a.length+b.length];
            
            int i=0,j=0,k=0;   //i:用于标示a数组    j:用来标示b数组  k:用来标示传入的数组

            while(i<a.length && j<b.length)
                if(a[i] <= b[j]) {
                    result[k++] = a[i++];
                }else{
                    result[k++] = b[j++];
                }

            /* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
            while(i < a.length) 
                result[k++] = a[i++];
            while(j < b.length)
                result[k++] = b[j++];
            
            return result;
        }
        else 
        {
            System.out.print("非有序数组,不可排序!");
            return null;
        }
    }
    
    //检查数组是否是顺序存储的
    public static boolean checkSort(int a[])
    {
for(int j=i+1; j<a.length; j++)
                if(a[j-1] > a[j])
                    return false;
        }
        return true;        
    }
    
    // 打印函数
    public static void print(int b[])
    {
         for(int i=0; i<b.length ; i++)
         {
             System.out.print(b[i] + (i%10 ==9 ? "\n":"\t"));
         }
    }
    
    public static void main(String args[])
    {
        int a[]={1,2,2,3,5,6,7,7};
        int b[]={1,2,4,5,8,8,9,10,11,12,12,13,14};
        int c[]= MergeList(a,b);
        if(c!=null)
        print(c);
        else
            System.out.println("");
    }
}

 

算法 - 两个有序数组合并成一个有序数组

标签:als   length   false   nbsp   turn   bsp   public   list   i++   

原文地址:http://www.cnblogs.com/clarke157/p/6806453.html

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