-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2011_code_double_divide.cpp
More file actions
33 lines (30 loc) · 963 Bytes
/
2011_code_double_divide.cpp
File metadata and controls
33 lines (30 loc) · 963 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
#include<stdio.h>
#include<stdlib.h>
int getMiddle(int a[], int start, int end) {
return a[(start + end) / 2];
}
int getDoubleMid(int s1[], int s2[], int length) {
int i = 0, log_i = 1;
while (log_i * 2 < length) { i++;log_i *= 2; }
int start_a = 0, start_b = 0, end_a = length - 1, end_b = length - 1;
while (i-- && start_a < end_a && start_b < end_b) {
int mid_a = getMiddle(s1, start_a, end_a);
int mid_b = getMiddle(s2, start_b, end_b);
if (mid_a < mid_b) {
start_a = (start_a + end_a) / 2 + 1;
end_b = (start_b + end_b) / 2 - 1;
}
else {
start_b = (start_b + end_b) / 2 + 1;
end_a = (start_a + end_a) / 2 - 1;
}
}
return s1[start_a] < s2[start_b] ? s1[start_a] : s2[start_b];
}
int main() {
int s1[] = { 1,2,3,8,9 };
int s2[] = { 4,5,6,7,10 };
int length = 5;
int ret = getDoubleMid(s1, s2, length);
return 0;
}