-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian.cpp
89 lines (81 loc) · 2.54 KB
/
median.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
using namespace std;
int recursionSearch(vector<int> numbers, int median, int start, int end);
int loopSearch(vector<int> numbers, int median);
int indexSearch(vector<int> numbers, int median);
// compare three versions of median search in a vector.
// the number list is sorted.
int main()
{
std::vector<int> numbers = {1, 2, 3, 4, 6, 7, 9, 10, 12, 14, 15, 16};
int median = 3; // first of all, we set a median which located in numbers
cout << "We find the median number at " << recursionSearch(numbers, median, 0, numbers.size() - 1)
<< " index by recursionSearch method." << endl;
cout << "We find the median number at " << loopSearch(numbers, median)
<< " index by loopSearch method." << endl;
cout << "We find the median number at " << indexSearch(numbers, median)
<< " index by indexSearch method." << endl;
median = 5; // then, set a median number not in numbers
cout << "We find the nearest median number at " << recursionSearch(numbers, median, 0, numbers.size() - 1)
<< " index by recursionSearch method." << endl;
cout << "We find the nearest median number at " << loopSearch(numbers, median)
<< " index by loopSearch method." << endl;
cout << "We find the nearest median number at " << indexSearch(numbers, median)
<< " index by indexSearch method." << endl;
}
/*
This method used recursion method to solve median number search in a number list.
*/
int recursionSearch(vector<int> numbers, int median, int start, int end)
{
if (start + 1 >= end) {
return start; // when two pointers meet together, still not find, return nearest index
}
int mid = start + (end - start) / 2;
if (numbers[mid] == median) {
return mid;
} else if (numbers[mid] > median) {
end = mid;
} else {
start = mid;
}
return recursionSearch(numbers, median, start, end);
}
/*
This method used loop search method to solve median number search in a number list.
*/
int loopSearch(vector<int> numbers, int median)
{
int start = 0;
int end = numbers.size();
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (numbers[mid] == median) {
return mid;
} else if (numbers[mid] > median) {
end = mid;
} else {
start = mid;
}
}
return start;
}
/*
This method use normal index method to search for median number in a number list.
Not a robust method, just act as an example.
*/
int indexSearch(vector<int> numbers, int median)
{
int i = 0;
while (i < numbers.size()) {
if (numbers[i] == median) {
return i;
} else if (numbers[i] > median) {
break;
} else {
i++;
}
}
return i - 1;
}