forked from Soumik-7031/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedArraysCpp
More file actions
35 lines (31 loc) · 901 Bytes
/
MergeTwoSortedArraysCpp
File metadata and controls
35 lines (31 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Function to find next gap.
int nextGap(int gap)
{
if (gap <= 1)
return 0;
return (gap / 2) + (gap % 2);
}
void merge(int* arr1, int* arr2, int n, int m)
{
int i, j, gap = n + m;
for (gap = nextGap(gap);
gap > 0; gap = nextGap(gap))
{
// comparing elements in the first array.
for (i = 0; i + gap < n; i++)
if (arr1[i] > arr1[i + gap])
swap(arr1[i], arr1[i + gap]);
// comparing elements in both arrays.
for (j = gap > n ? gap - n : 0;
i < n && j < m;
i++, j++)
if (arr1[i] > arr2[j])
swap(arr1[i], arr2[j]);
if (j < m) {
// comparing elements in the second array.
for (j = 0; j + gap < m; j++)
if (arr2[j] > arr2[j + gap])
swap(arr2[j], arr2[j + gap]);
}
}
}