| 13 | return gap / 2 + gap % 2; |
| 14 | } |
| 15 | void merge(long long arr1[], long long arr2[], int n, int m) |
| 16 | { |
| 17 | int i, j, gap = m + n; |
| 18 | |
| 19 | //we traverse the array till our gap became 0.. |
| 20 | for (gap = newGap(gap); gap > 0; gap = newGap(gap)) |
| 21 | { |
| 22 | |
| 23 | //now check the condition when both compared elements are in first array... |
| 24 | for (i = 0; i + gap < n; i++) |
| 25 | { |
| 26 | if (arr1[i] > arr1[i + gap]) |
| 27 | { |
| 28 | swap(arr1[i], arr1[i + gap]); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | //when compared elements are in first and second both array... |
| 33 | for (j = 0; j < m && i < n; i++, j++) |
| 34 | { |
| 35 | if (arr1[i] > arr2[j]) |
| 36 | swap(arr1[i], arr2[j]); |
| 37 | } |
| 38 | |
| 39 | //when compared element are in second array... |
| 40 | for (j = 0; j + gap < m; j++) |
| 41 | { |
| 42 | if (arr2[j] > arr2[j + gap]) |
| 43 | swap(arr2[j], arr2[j + gap]); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | int main() |
| 49 | { |