Skip to content

Instantly share code, notes, and snippets.

@agatsoh
Last active Jun 23, 2019
Embed
What would you like to do?
#include <iostream>
using std::cout;
using std::cin;
struct Array {
int *A;
int size;
int length;
};
void display(Array *arr) {
for(int i = 0; i < arr->length; i++) {
cout << arr->A[i] << "\t";
}
cout << "\n";
}
Array* merge(Array *arr, Array *brr) {
Array *crr = new Array;
crr->size = arr->length + brr->length;
crr->A = new int[crr->size];
int i, j, k; i=j=k=0;
while(i < arr->length && j < brr->length) {
if (arr->A[i] < brr->A[j]) {
crr->A[k++] = arr->A[i++];
} else {
crr->A[k++] = brr->A[j++];
}
}
for(;i < arr->length; i++) {
crr->A[k++] = arr->A[i];
}
for(;j < brr->length; j++) {
crr->A[k++] = brr->A[j];
}
crr->length = arr->length + brr->length;
return crr;
}
int main() {
Array arr1 = {new int[10]{ 2, 6, 10, 15, 25}, 10, 5};
Array arr2 = {new int[10]{ 3, 4, 7, 18, 20}, 10, 5};
Array *arr3;
arr3 = merge(&arr1, &arr2);
display(arr3);
delete []arr1.A;
delete []arr2.A;
delete []arr3->A;
delete arr3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment