In C++, there is a technical problem, in the sense that three arrays result instead of the one new merged array. Would it not be nice to delete the old two arrays after merging, and free unused memory? C++ has two ways of having two arrays merged: if the two arrays merged, used dynamic memory, then they can be deleted to end up with one array; otherwise, the programmer ends up with three arrays.
Merging arrays by ultimately just fitting one at the back of the other is good; but it can be better to have some minimal sorting as the arrays are merged. Sorting as a whole, is a whole topic in programming. Sorting as a whole is not addressed in this article. However, a very simple minimal sorting is addressed.
This article explains how to merge two arrays, to end up with three arrays, and how to merge two arrays to end up with one array. Some minimal sorting is also considered. To merge two arrays, the two arrays must be of the same type.
The procedure of merging two arrays, can be extended to more than two arrays.
Article Content
Merging Arrays Without Free Store
Merging without Sorting
Consider the following two arrays:
char arr2[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
The first one has 5 elements and the second one has 8 elements. If the elements of the second array are somehow fitted to the back of the first array, an array of 13 elements will be formed. In order to achieve this without using free store (dynamic memory), a third array of 13 empty values has to be created first. Then the 5 values of the first array will be copied, to the first 5 locations of the third array. The 8 values of the second array will next be copied to the remaining 8 positions of the third array. The third array becomes the merged and desired array. The following program illustrates this:
using namespace std;
int main()
{
char arr1[] = {'I', 'J', 'K', 'L', 'M'};
char arr2[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
char arr3[13];
for (int i=0; i< 5; i++) {
arr3[i] = arr1[i];
}
for (int i=5; i< 13; i++) {
arr3[i] = arr2[i-5];
}
for (int i=0; i< 13; i++) {
cout<< arr3[i] << ' ';
}
cout<<endl;
return 0;
}
The output is:
Note how indexing has been used in the for-loops. The problem with this scheme is that the first two arrays have become redundant. They are now occupying the computer’s memory unnecessarily. Without free store (dynamic memory), arrays cannot be removed from memory until they go out of scope. To solve this problem, use free store – see below.
The first code segment includes the iostream library and declares the use of the standard namespace for the rest of the program. The rest of the program is in the main() function. The first three statements in the main() function declares the first, second and third arrays. The next code segment is a for-loop that copies all the elements from the smaller array to the third array. The bigger array of the first two, could have been copied first; it does not matter.
The next code segment uses the for-loop to copy the bigger array to the back of the smaller array already in the third array. The third array is the merged array. The sum of the number of elements in the first two arrays should equal the number of elements in the third array. The last code segment displays the values in the third array.
Merging with Some Sorting
While inserting elements into the third array, at the beginning, the first elements of both arrays can be compared and the smaller value inserted first before the first value of the other array. The second elements of both arrays can be compared next, and the smaller value inserted into the third array, before the second value of the other array, is inserted. The third elements of both arrays can be compared next, and the smaller value inserted before the third value of the other array. This procedure continues until all the elements of the shorter array are inserted alongside the same number of elements of the longer array. The rest of the elements of the longer array can just be pushed into the third array in their order. The following program illustrates this:
using namespace std;
int main()
{
char arr1[] = {'I', 'J', 'K', 'L', 'M'};
char arr2[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
char arr3[13];
for (int i=0; i< 5; i++) {
if (arr1[i] < arr2[i]) {
arr3[i*2] = arr1[i];
arr3[i*2+1] = arr2[i];
}
else {
arr3[i*2] = arr2[i];
arr3[i*2+1] = arr1[i];
}
}
for (int i=5; i< 8; i++) {
arr3[i+5] = arr2[i];
}
for (int i=0; i< 13; i++) {
cout<< arr3[i] << ' ';
}
cout<<endl;
return 0;
}
The output is:
Note the arithmetic used in the indexes.
Merging Arrays Using Free Store
Merging Without Sorting
Free store is memory allocated to a program to be used when it needs extra memory. An array can be created and deleted in free store with the new[] operator and the delete[] operator, respectively. The above two programs will be repeated below. The first and second arrays will be created dynamically in free store, and be deleted after the third merged array has been made. The third array will be created in normal memory (area).
The following program illustrates this for merging without sorting:
using namespace std;
int main()
{
char *arr1 = new char[5];
arr1[0] = 'I'; arr1[1] = 'J'; arr1[2] = 'K'; arr1[3] = 'L'; arr1[4] = 'M';
char *arr2 = new char[8];
arr2[0] = 'A'; arr2[1] = 'B'; arr2[2] = 'C'; arr2[3] = 'D'; arr2[4] = 'E'; arr2[5] = 'F'; arr2[6] = 'G'; arr2[7] = 'H';
char arr3[13];
//merging
for (int i=0; i< 5; i++) {
arr3[i] = arr1[i];
}
for (int i=5; i< 13; i++) {
arr3[i] = arr2[i-5];
}
delete[] arr1;
delete[] arr2;
for (int i=0; i< 13; i++) {
cout<< arr3[i] << ' ';
}
cout<<endl;
return 0;
}
The output is:
The name of arrays in free store are pointers. The locations of the elements of arr1 and arr2 were deleted after their use in the program. The rest of the code is like a previous one.
Merging with some Sorting
The previous program with some sorting is repeated here. However, here, the first and second arrays are created in free store. They are deleted after their use. The program is:
using namespace std;
int main()
{
char *arr1 = new char[5];
arr1[0] = 'I'; arr1[1] = 'J'; arr1[2] = 'K'; arr1[3] = 'L'; arr1[4] = 'M';
char *arr2 = new char[8];
arr2[0] = 'A'; arr2[1] = 'B'; arr2[2] = 'C'; arr2[3] = 'D'; arr2[4] = 'E'; arr2[5] = 'F'; arr2[6] = 'G'; arr2[7] = 'H';
char arr3[13];
//merging
for (int i=0; i< 5; i++) {
if (arr1[i] < arr2[i]) {
arr3[i*2] = arr1[i];
arr3[i*2+1] = arr2[i];
}
else {
arr3[i*2] = arr2[i];
arr3[i*2+1] = arr1[i];
}
}
for (int i=5; i< 8; i++) {
arr3[i+5] = arr2[i];
}
delete[] arr1;
delete[] arr2;
for (int i=0; i< 13; i++) {
cout<< arr3[i] << ' ';
}
cout<<endl;
return 0;
}
The output is:
Conclusion
Merging arrays is actually a simple thing. Just ultimately fit one array at the back of the other array, and you have merged the two arrays. The problems programmers face with merging arrays, are not to do with fitting one array at the back of another array. They are to do with erasing the previous two arrays and/or sorting the merged array. Arrays must be of the same type, in order to be merged.
If any of the first two arrays will no longer be needed after merging, then it should be created dynamically in free store, and then be deleted after use, to free memory. The merged array can also be created in free store, but that is not necessary.
The merged array can be sorted to different extents. Complete sorting is a whole topic in computer programming. Complete sorting is of different schemes in computer programming. There is a scheme called merge-sort. This scheme does the merging and sorting at the same time. However, the most popular scheme seems to be quicksort.