Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 10 / example3.cpp
blob: db6902b44e1c5cafc1b6574c1ea2b1d48ea18bb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

void merge(int* arr1, unsigned size1, const int* arr2, unsigned size2)
{
	int* result = new int[size1 + size2];

	unsigned iter1 = 0;
	unsigned iter2 = 0;
	unsigned resultIter = 0;

	while (iter1 < size1 && iter2 < size2)
	{
		if (arr1[iter1] <= arr2[iter2])
			result[resultIter++] = arr1[iter1++];
		else
			result[resultIter++] = arr2[iter2++];
	}

	while (iter1 < size1)
		result[resultIter++] = arr1[iter1++];

	while (iter2 < size2)
		result[resultIter++] = arr2[iter2++];

	memcpy(arr1, result, sizeof(int) * (size1+ size2));
	delete[] result;
}

int main()
{

	int arr[9] = { 1, 3, 5, 10 };
	int arr2[] = { 2, 4, 6, 100, 9900 };
	merge(arr, 4, arr2, 5);

	for (int i = 0; i < 9; i++)
		cout << arr[i] << " ";
}