Guest User

Untitled

a guest
Jun 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int * Merge (int a[], int size_a, int b[], int size_b);
  5.  
  6. int main() {
  7. int a[] = {1, 5, 8, 9, 13};
  8. int b[] = {2, 6};
  9.  
  10. int * c = Merge(a, 5, b, 2);
  11. for(int i = 0; i < 7; i++) {
  12. cout << c[i] << " ";
  13. }
  14.  
  15. cout << endl;
  16. }
  17.  
  18. int * Merge (int a[], int size_a, int b[], int size_b) {
  19.  
  20. // new array the combined size of the two previous ones
  21. int * c = new int[size_a + size_b];
  22.  
  23. //indexes so we don't fuck around with pointer math
  24. int index_a = 0, index_b = 0, index_c = 0;
  25.  
  26. while ((index_a < size_a) && (index_b < size_b)) {
  27.  
  28. if (a[index_a] < b[index_b]) {
  29. c[index_c] = a[index_a];
  30. index_a++;
  31. } else {
  32. c[index_c] = b[index_b];
  33. index_b++;
  34. }
  35. index_c++;
  36. }
  37.  
  38. while (index_a < size_a) {
  39. c[index_c] = a[index_a];
  40. index_a++;
  41. index_c++;
  42. }
  43.  
  44. while (index_b < size_b) {
  45. c[index_c] = b[index_b];
  46. index_b++;
  47. index_c++;
  48. }
  49.  
  50. return c;
  51. }
Add Comment
Please, Sign In to add comment