一:
题目,给定非递减的数组,组合成一个非递减的数组,如数组A:1,2,2,3。B:1,3,4。组合后的数组为:1,1,2,2,3,3,4.
我们知道,数组排序评价标准是时间复杂度。
废话不多说 ,上代码:
平台是VS2008
include"iostream"using namespace std;
void AdjustArray(const int * ,const int ,const int *,const int, int* );
int main()
{
int OneArray[4]={1,2,2,3};
int TwoArray[3]={1,3,4};
int* Array=(int*)malloc(7*sizeof(OneArray[0]));
if(!Array) throw "The Pointer is null";
AdjustArray(OneArray,4,TwoArray,3,Array);
for(int k=0;k<7;++k)
cout<<Array[k]<<",";
}
void AdjustArray(const int * SrcArray1,const int SrcArr1Length,const int *SrcArray2,const int SrcArr2Length, int* DstArray)
{
int i=0;
int j=0;
while(i<SrcArr1Length && j<SrcArr2Length)
{
if(SrcArray1[i]<SrcArray2[j])
{
DstArray[i+j]=SrcArray1[i];
++i;
}
else
{
DstArray[i+j]=SrcArray2[j];
++j;
}
}
while(j<SrcArr2Length)
{
DstArray[i+j]=SrcArray2[j];
++j;
}
while(i<SrcArr1Length)
{
DstArray[i+j]=SrcArray1[i];
++i;
}
}
评价一个算法好坏的方法是时间复杂度,次算法的时间复杂度为:O(M+N)。可以接受。
运行结果为: