Skip to content

Instantly share code, notes, and snippets.

@LaBlazer
Created November 6, 2019 11:21
Show Gist options
  • Save LaBlazer/40b21f36261dda34f6d945607d3632f6 to your computer and use it in GitHub Desktop.
Save LaBlazer/40b21f36261dda34f6d945607d3632f6 to your computer and use it in GitHub Desktop.
template<typename T>
void mergeArrays(T arr1[], T arr2[], int n1,
int n2, T arr3[])
{
int i = 0, j = 0, k = 0;
// Traverse both array
while (i<n1 && j <n2)
{
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
// Store remaining elements of first array
while (i < n1)
arr3[k++] = arr1[i++];
// Store remaining elements of second array
while (j < n2)
arr3[k++] = arr2[j++];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment