You are given two sorted arrays (all elements are in ascending order). You need to merge these
two arrays such that the initial numbers (after complete sorting) are in the first array and the
remaining numbers are in the second array.
#include <iostream>
void sort(int* array, int count)
{
int i, j, temp;
for (i = 0; i < count - 1; i++)
for (j = 0; j < count - 1 - i; j++)
if (*(array + j) > *(array + j + 1))
{
temp = *(array + j + 1);
*(array + j + 1) = *(array + j);
*(array + j) = temp;
}
}
int main()
{
const int count = 10;
int* A; int* B; int* C; int i;
A = new int[count];
B = new int[count];
C = new int[count * 2];
std::cout << "Array A:\n";
for (i = 0; i < count; i++)
{
*(A + i) = 1 + i * 2;
*(C + i) = *(A + i);
std::cout << *(A + i) << " ";
}
std::cout << "\nArray B:\n";
for (i = 0; i < count; i++)
{
*(B + i) = 2 + i * 2;
*(C + i + count) = *(B + i);
std::cout << *(B + i) << " ";
}
std::cout << "\nArray C:\n";
for (i = 0; i < count * 2; i++)
std::cout << *(C + i) << " ";
std::cout << "\nSorted array C:\n";
sort(C, count * 2);
for (i = 0; i < count * 2; i++)
std::cout << *(C + i) << " ";
for (i = 0; i < count; i++)
{
*(B + i) = *(C + i + count);
*(A + i) = *(C + i);
}
std::cout << "\nArray A:\n";
for (i = 0; i < count; i++)
std::cout << *(A + i) << " ";
std::cout << "\nArray B:\n";
for (i = 0; i < count; i++)
std::cout << *(B + i) << " ";
}
Comments
Leave a comment