-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchBlock.cc
More file actions
73 lines (62 loc) · 1.69 KB
/
SearchBlock.cc
File metadata and controls
73 lines (62 loc) · 1.69 KB
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
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <span>
#include "vector.hh"
// 线性查找
template <typename compar>
int linearSearch(const compar &key, std::span<const compar> A) {
for (int i(0), sz(A.size()); i < sz; ++i)
if (A[i] == key) return i;
return -1;
}
// 分块查找
template <typename compar>
int blockSearch(const compar &key, std::span<const compar> A) {
assert(std::ranges::is_sorted(A));
int n = A.size(), block = 1;
while (block * block <= n) block++;
int hi = 0;
for (; hi < n; hi += block) {
if (key < A[hi]) break;
}
if (hi > 0) {
int i{hi - block};
if (n <= hi) hi = n;
for (; i < hi; i++)
if (key == A[i]) return i;
}
return -1;
}
int main() {
std::srand(std::time(nullptr));
ns::vector<int> vec{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29};
int num{std::rand() % vec.back()};
std::cout << "target\t" << num << '\n';
std::cout << "vector\t";
for (const auto &e : vec) std::cout << e << '\t';
std::cout << '\n';
int r{linearSearch<int>(num, vec)};
std::cout << "linear\t";
if (r != -1) {
for (int i = 0; i < r; ++i) std::cout << '\t';
std::cout << vec[r];
} else
std::cout << r;
std::cout << '\n';
assert(linearSearch<int>(vec.front() - 1, vec) == -1);
assert(linearSearch<int>(vec.back() + 1, vec) == -1);
int s{blockSearch<int>(num, vec)};
std::cout << "block\t";
if (s != -1) {
for (int i = 0; i < s; ++i) std::cout << '\t';
std::cout << vec[s];
} else
std::cout << s;
std::cout << '\n';
assert(blockSearch<int>(vec.front() - 1, vec) == -1);
assert(blockSearch<int>(vec.back() + 1, vec) == -1);
assert(r == s);
}