Created
August 19, 2016 08:06
-
-
Save itayB/5d6815fa54d48b1bc639db0003c9ab5a to your computer and use it in GitHub Desktop.
This file contains 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 namespace std; | |
#define MAX_A 10 | |
#define MAX_B 4 | |
void sortedMerge(int a[], int b[]) { | |
int pA = MAX_A - MAX_B - 1; | |
int pB = MAX_B - 1; | |
int pS = MAX_A - 1; | |
while (pB >= 0) { | |
if (pA >= 0 && a[pA] > b[pB]) { | |
a[pS] = a[pA]; | |
pA--; | |
} | |
else { | |
a[pS] = b[pB]; | |
pB--; | |
} | |
pS--; | |
} | |
} | |
void printA(int a[]) { | |
cout << "["; | |
for (int i=0 ; i < MAX_A ; i++) { | |
if (i) | |
cout << ","; | |
cout << a[i]; | |
} | |
cout << "]" << endl; | |
} | |
void test(int a[], int b[]) { | |
sortedMerge(a,b); | |
printA(a); | |
} | |
int main() { | |
int a[] = {1,2,3,4,5,9,0,0,0,0}; | |
int b[] = {6,7,8,10}; | |
test(a,b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment