合并两个数组

#include <iostream>
using namespace std;
int sortedInsert(int A[],int lenA,int B[],int lenB)
{
    if(B == NULL)
        return lenA;
    int len = lenA + lenB;
    int indexNew = len-1;
    int indexA = lenA-1;
    int indexB = lenB-1;
    while( indexA>=0 && indexB>=0 )
    {
        if(A[indexA] > B[indexB])
        {
            A[indexNew] = A[indexA--];
            
        }else{
            A[indexNew] = B[indexB--];    
        }
    //    printf("%d \n",A[indexNew]);
        //printf("here lenA = %d,lenB = %d\n",indexA,indexB) ;
        indexNew --;
    }
    //下面处理剩余的那部分;
    //printf("here lenA = %d,lenB = %d\n",indexA,indexB) ;
    while(indexA>=0)
    {
        //
        A[indexNew] = A[indexA];
        //printf("%d ",A[indexNew]);
        indexNew--; indexA -- ;
    } 
    //printf("OK");
    while(indexB>=0)
    {
        
        A[indexNew] = B[indexB];
        //printf("%d ",A[indexNew]);
        indexNew--; indexB -- ;
    } 
    return lenA+lenB;
}
int intCmp(int * A ,int lenA,int * expected ,int lenExpected)
{
    if(lenA != lenExpected)
        return -1;
    for(int i = 0;i<lenA;i++)
        if(A[i]!=expected[i])
        {
            //printf("%d %d %d\n",i,A[i],expected[i]);
            return -1;
        }
    return 0;
}
void test(char * testNumber,int *A,int lenA,int *B,int lenB,int* expected,int lenExpected)
{
    lenA = sortedInsert(A,lenA,B,lenB);
    printf("%s ",testNumber); 
    if(intCmp(A,lenA,expected,lenExpected) == 0 )
        printf("passed\n");
    else
        printf("failed\n");
}
void test1()//B插在A的后头; 
{
    int A[100]={1,3,5,7,9};
    int B[]={2,4,6,8};
    //sortedInsert(A,5,B,4);
    int expected[]={1,2,3,4,5,6,7,8,9};
    test("test1",A,5,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
} 
void test2()//B要插在A的前头; 
{
    int A[100]={6,7,8,9};
    int B[]={1,2,3,4,5};
    //sortedInsert(A,5,B,4);
    int expected[]={1,2,3,4,5,6,7,8,9};
    test("test2",A,4,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
} 
void test3()//A没有内容 
{
    int A[100]={};
    int B[]={1,3,5,7,9};
    //sortedInsert(A,5,B,4);
    int expected[]={1,3,5,7,9};
    test("test3",A,0,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
} 
void test4()//B没有内容 
{
    int A[100]={1,3,5,7,9};
    int B[]={};
    //sortedInsert(A,5,B,4);
    int expected[]={1,3,5,7,9};
    test("test4",A,5,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
}  
void test5()//A,B都没有内容 
{
    int A[100]={};
    int B[]={};
    //sortedInsert(A,5,B,4);
    int expected[]={};
    test("test5",A,0,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
} 
void test6()//B为空指针 
{
    int A[100]={1,3,5,7,9};
    int * B = NULL; 
    //sortedInsert(A,5,B,4);
    int expected[]={1,3,5,7,9};
    test("test6",A,5,B,sizeof(B)/sizeof(int),expected,sizeof(expected)/sizeof(int));
} 
int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    return 0;
}

 

 合并两个数组(包括字符串)时,如果从前往后复制每个数字(或字符)需要重复移动数字(或字符)多次,那么我们可以考虑从后往前复制,这样就能减少移动的次数,从而提高效率。

 

posted @ 2015-03-12 23:40  在河之博  阅读(432)  评论(0编辑  收藏  举报