Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Merge Arrays/c++/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//Merging two arrays
#include <iostream>

using namespace std;

void print(int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}

void sort(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}

void merge(int a[], int b[], int res[], int n, int m)
{
int i = 0, j = 0, k = 0;
while (i < n)
{
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m)
{
res[k] = b[j];
j += 1;
k += 1;
}
sort(res, n + m);
}

int main()
{
int a[] = {1, 4, 9, 11, 8};
int b[] = {13, 21, 3, 6, 19};
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);
cout << "First Array : ";
print(a, n);
cout << "Second Array : ";
print(b, m);
int res[m + n];
merge(a, b, res, n, m);
cout << "Merged Array : ";
print(res, n + m);
return 0;
}