数据结构-练习1 数组排序

一:

   题目,给定非递减的数组,组合成一个非递减的数组,如数组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)。可以接受。

运行结果为:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值