Last active
June 23, 2019 17:45
-
-
Save agatsoh/19aa30586137a4767dba51f0c13ec2c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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