有序顺序表合并

#include <iostream>
using namespace std;
#define MAX 100
void merge(int arr1[],int len1,int arr2[],int len2,int arr3[],int len3) 
{
    len3--; 
    len1--; 
    len2--; 
    while(len1 >= 0 && len2 >= 0) 
    { 
       if (arr1[len1] >= arr2[len2]) 
       { 
          arr3[len3--] = arr1[len1--]; 
       } 
       else 
       { 
          arr3[len3--] = arr2[len2--]; 
       } 
    } 

    while (len1 >= 0) 
    { 
       arr3[len3--] = arr1[len1--]; 
    }

    while (len2 >= 0) 
    { 
       arr3[len3--] = arr2[len2--]; 
    } 
}
void main()
{   

    int arr1[] = {1,2,4,5,6};
    int arr2[] = {2,3,4,9};
    int len1 = sizeof(arr1)/sizeof(int);
    int len2 = sizeof(arr2)/sizeof(int);
    int len3 = len1 +len2;
    int arr3[MAX];
    cout<<"合并前:"<<endl;
    for(int i = 0;i<len1;i++)
        cout<<arr1[i]<<" ";
    cout<<endl;
    for(i = 0;i<len2;i++)
        cout<<arr2[i]<<" ";
    cout<<endl;
    merge(arr1,len1,arr2,len2,arr3,len3);
    cout<<"合并后:"<<endl;
    for(i = 0;i<len1+len2;i++)
        cout<<arr3[i]<<" ";
    cout<<endl;
}
上一篇:标准输入流


下一篇:回文检测(包含中文与字符)