两个有序数组合并为一个有序数组

先依次比较两个数组,按照小的就传入新的数组。当这次比较完之后可能有一个数组的长度很长,留下一些数组,然后在新数组的末尾插入即可。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 class ArraySort
{
    //两个有序数组的合并函数
    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[j])
                    return false;
                else change = false;
        }
        return true;        
    }
    
    // 打印函数
    public static void print(int b[])
    {
         for(int i=0; i


你可能感兴趣的:(Java)