Thursday, June 29, 2017

merge

int* mergeTwo(int arr1[], int arr2[], int len1, int len2)
{
  int j=0, k=0;
  int* newArr = malloc((len1+len2)*sizeof(int));
  int newLen = len1+len2;
  for(int i=0; i<newLen; i++){
    if(j<len1 && k<len2){
    if (arr1[j] <= arr2[k]){
              newArr[i] = arr1[j];
              j++;
    }else{
        newArr[i] = arr2[k];
        k++;
    }
    }else if(j>=len1){
      newArr[i]=arr2[k];
      k++;
    }else{
    newArr[i]=arr1[j];
    j++;
    }
  }
  return newArr;
}

No comments:

Post a Comment