【发布时间】:2020-05-14 12:59:07
【问题描述】:
我正在做一个项目,我使用二进制搜索来查看向量。当我输出数据时,线性搜索正确完成,但二进制搜索没有。
输出: ------ 数据来源:132个唯一随机数 ------ 80 81 67 20 187 90 53 13 103 100 83 11 198 124 171 52 68 2 130 172 50 65 102 38 117 196 127 140 22 42 99 120 9 158 115 192 169 43 186 69 176 55 182 15 143 37 23 151 41 164 48 77 183 1 54 79 5 199 93 141 33 150 162 56 110 24 6 173 44 154 78 85 112 200 144 166 72 195 126 35 64 193 177 47 131 94 88 137 96 95 160 122 111 165 108 49 184 4 116 66 138 8 142 16 63 148 57 70 170 21 59 159 139 75 58 174 180 25 152 132 121 194 82 190 71 181 97 10 106 39 178 46 ------ 要搜索的 100 个随机数: ------ 66 158 40 148 80 160 146 112 154 128 2 166 180 177 183 179 9 78 83 24 107 162 54 149 84 134 9 151 200 179 197 18 137 37 117 168 196 62 79 102 142 32 67 121 8 1 51 168 78 85 191 136 46 196 85 81 130 45 31 81 23 180 98 159 16 166 126 163 28 156 64 121 187 82 41 147 83 44 114 112 128 105 48 126 52 84 158 181 128 141 62 102 120 111 61 87 77 138 49 56
线性搜索:找到 100 个数字。每次搜索的平均比较次数:52
二分查找:找到 0 个数字。每次搜索的平均比较次数:7
------- 数据源中的数字现在已排序 ---------
线性搜索:找到 100 个数字。每次搜索的平均比较次数:47.41
二分查找:找到 0 个数字。每次搜索的平均比较次数:7
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
const int DATA_SIZE = 200;
const int DATA_RANGE = 200;
const int DATA_SEED = 9;
const int SEARCH_SEED = 17;
using namespace std;
int linear_search(const vector<int>& inputVec, const int x, int&
comparisons) {
comparisons = 0;
for (size_t i = 0; i < inputVec.size();i++)
{
comparisons++;
if (x == inputVec[i])
return i;
if (x >= inputVec.size())
return 0;
}
}
int binary_search(const vector<int>& inputVec, const int x,
int& comparisons) {
int mid = 0;
int beg = 0;
int end = inputVec.size();
comparisons = 0;
for (size_t x = 0; beg <= end; x++) {
mid = (end + beg) / 2;
comparisons++;
if (x == inputVec[mid]) {
return x;
}
else if (x < inputVec[mid])
end = mid-1;
else
beg = mid+1;
}
return -1;
}
void print_vec(const vector<int>& vec) {
vector<int>::const_iterator begin = vec.begin();
int line = 0;
cout << setw(6);
for (size_t i = 0; i < vec.size(); begin++,i++)
{
if ((*begin / 5) < 1)
cout << *begin << setw(6);
else if ((*begin / 10) == 5)
cout << *begin << setw(6);
else
cout << *begin << setw(6);
line++;
if (line == 5){
line = 0;
cout << endl;
cout << setw(6);
}
}
}
void average_comparisons(const vector<int>& inputVec, const
vector<int>& searchVec, int(*search)(const vector<int>&, const
int, int&)) {
int i, comparison = 0, sum = 0, found = 0;
int res = 0;
for (i = 0; i < (int)searchVec.size(); i++) {
res = search(inputVec, searchVec[i], comparison);
sum += comparison;
if (res >= 0)
found++;
}
cout << found << " numbers are found. The average number of
comparisons in each search: " << (double)sum /
(double)searchVec.size() << endl << endl;
}
int random_number() {
return rand() % DATA_RANGE + 1;
}
int main() {
// -------- create unique random numbers ------------------//
vector<int> inputVec(DATA_SIZE);
srand(DATA_SEED);
generate(inputVec.begin(), inputVec.end(), random_number);
sort(inputVec.begin(), inputVec.end());
vector<int>::iterator it = unique(inputVec.begin(),
inputVec.end());
inputVec.resize(it - inputVec.begin());
random_shuffle(inputVec.begin(), inputVec.end());
cout << "------ Data source: " << inputVec.size() << " unique
random numbers ------" << endl;
print_vec(inputVec);
cout << endl;
// -------- create random numbers to be searched ---------//
vector<int> searchVec(DATA_SIZE / 2);
srand(SEARCH_SEED);
generate(searchVec.begin(), searchVec.end(), random_number);
cout << "------ " << searchVec.size() << " random numbers to be
searched: ------" << endl;
print_vec(searchVec);
cout << endl;
cout << "Linear search: ";
average_comparisons(inputVec, searchVec, linear_search);
cout << "Binary search: ";
average_comparisons(inputVec, searchVec, binary_search);
sort(inputVec.begin(), inputVec.end());
cout << "------- numbers in data source are now sorted ---------"
<< endl << endl;
cout << "Linear search: ";
average_comparisons(inputVec, searchVec, linear_search);
cout << "Binary search: ";
average_comparisons(inputVec, searchVec, binary_search);
return 0;
}
【问题讨论】:
-
在
binary_loop函数中,变量x被遮蔽了。将循环中的变量重命名为其他名称。 -
而且,除了遮蔽之外,binary_search 函数中的
for循环没有任何意义。此外,每个自尊的 C++ 编译器都会对线性搜索功能发出警告。线性搜索功能也被破坏了。 -
二进制搜索不适用于随机打乱的容器,因此在排序之前运行它没有多大意义。
标签: c++