C++實現兩個有序的數組合並,組成新的有序數組


 

#include "stdafx.h"
#include<iostream>
using namespace std;

int *SortArry(int *StrA,int lenA, int *StrB, int lenB)
{
 if (StrA == NULL || StrB == NULL)
  return NULL;

 int *StrC = new int[lenA + lenB+1];
 int i, j, k; i = j = k = 0;
 while (i < lenA && j < lenB)
 {
  if (StrA[i] < StrB[j])
   StrC[k++] = StrA[i++];
  else
  {
   StrC[k++] = StrB[j++];
  }
 }

 while (i<lenA)
 {
  StrC[k++] = StrA[i++];
 }

 while (j<lenB)
 {
  StrC[k++] = StrB[j++];
 }

 return StrC;
}
int main()
{
 int array1[] = { 1, 4, 6, 7, 9, 12 };
 int array2[] = { 2, 3, 4, 5, 6, 7, 8, 10 };
 int lenA = sizeof(array1) / sizeof(int);
 int lenB = sizeof(array2) / sizeof(int);
 int *ret = SortArry(array1, lenA, array2, lenB);
 for (int i = 0; i < (lenA + lenB); i++)
  cout << ret[i] <<endl;
 delete[] ret; //清空堆內存
 return 0;
}

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM