-
Notifications
You must be signed in to change notification settings - Fork 1
/
10.binary_search.cpp
129 lines (119 loc) · 3.08 KB
/
10.binary_search.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @file Name : 10.binary_search.cpp
* @brief :
* @author : Ren Boyu
* @mail : [email protected]
* @created Time : 五 4/28 18:39:21 2023
*/
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <ctime>
using namespace std;
void output_binary_search(int *arr, int n, int head, int tail, int mid) {
int p1, p2, p3, len = 0;
for (int i = 0; i < n; i++) {
len += printf("%5d", i);
if (i == head) p1 = len - 1;
if (i == tail) p2 = len - 1;
if (i == mid) p3 = len - 1;
}
printf("\n");
for (int i = 0; i < len; i++) printf("-");
printf("\n");
for (int i = 0; i < n; i++) {
printf("%5d", arr[i]);
}
printf("\n");
for (int i = 0; i < len; i++) {
if (i == p1 || i == p2 || i == p3) {
printf("^");
} else {
printf(" ");
}
}
printf("\n");
for (int i = 0; i < len; i++) {
if (i == p1 || i == p2 || i == p3) {
printf("|");
} else {
printf(" ");
}
}
printf("\n\n");
return ;
}
int binary_search(int *arr, int n, int x) {
int head = 0, tail = n - 1, mid;
// 为了防止基数偶数的问题
// 和防止出现死循环
// 可以这样写比较简单
// 01 10
// 也可以用
while (tail - head > 3) {
// while (head <= tail) {
// haed tail 同时很大的时候
// 就会越界了
// mid = (head + tail) >> 1;
// 可以这样修改
mid = head + ((tail - head) / 2);
output_binary_search(arr, n, head, tail, mid);
if (arr[mid] == x) return mid;
if (arr[mid] < x) head = mid + 1;
else tail = mid - 1;
}
for (int i = head; i <= tail; i++) {
if (arr[i] == x) return i;
}
return -1;
}
int binary_search_01(int *arr, int n, int x) {
int head = 0, tail = n - 1, mid;
// 因为是找第一个1出现的位置
// 那个时候必定head tail重合
while (head < tail) {
// haed tail 同时很大的时候
// 就会越界了
// mid = (head + tail) >> 1;
// 可以这样修改
mid = head + (tail - head) / 2;
output_binary_search(arr, n, head, tail, mid);
if (arr[mid] < x) head = mid + 1;
// mid == 1不能跳过哦
else tail = mid;
}
return head;
}
int *get_data(int n) {
int *arr = (int *)malloc(sizeof(int) * n);
arr[0] = rand() % 10;
for (int i = 1; i < n; i++) {
arr[i] = arr[i - 1] + rand() % 5 + 1;
}
return arr;
}
void output(int *arr, int n) {
int len = 0;
for (int i = 0; i < n; i++) {
len += printf("%5d", i);
}
printf("\n");
for (int i = 0; i < len; i++) printf("-");
printf("\n");
for (int i = 0; i < n; i++) {
printf("%5d", arr[i]);
}
printf("\n");
return ;
}
int main() {
srand(time(0));
int n, x;
scanf("%d", &n);
int *arr = get_data(n);
output(arr, n);
while (~scanf("%d", &x)) {
printf("arr[%d] = %d\n", binary_search_01(arr, n, x), x);
}
return 0;
}