-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.1.cpp
More file actions
80 lines (74 loc) · 1.62 KB
/
8.1.cpp
File metadata and controls
80 lines (74 loc) · 1.62 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
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
typedef int KeyType;
typedef struct
{
KeyType key;
} RecType;
// 顺序查找
int SeqSearch(RecType R[], int n, KeyType k)
{
int i = 0;
R[n].key = k;
printf("顺序查找:\n");
while (R[i].key != k)
{
printf("第%d次:%d\n", i + 1, R[i].key);
i++;
}
printf("第%d次:%d\n", i + 1, R[i].key);
if (i == n)
{
printf("\n顺序查找结果:元素不存在\n");
return 0;
}
else
{
printf("\n顺序查找成功,其逻辑位序为:%d\n", i + 1);
return i + 1;
}
}
// 二分查找
int BinSearch(RecType R[], int n, KeyType k)
{
int low = 0, high = n - 1, mid;
printf("二分查找:\n");
int count = 1;
while (low <= high)
{
mid = (low + high) / 2;
printf("第%d次:%d\n", count, R[mid].key);
count++;
if (R[mid].key == k)
{
printf("\n二分查找成功,其逻辑位序为:%d\n", mid + 1);
return mid + 1;
}
else if (k < R[mid].key)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
printf("\n元素不存在\n");
return 0;
}
int main()
{
KeyType keys[] = {3, 4, 5, 7, 24, 30, 42, 54, 63, 72, 87, 95};
int n = sizeof(keys) / sizeof(KeyType);
RecType R[n + 1];
for (int i = 0; i < n; i++)
{
R[i].key = keys[i];
}
scanf("%d", &R[n].key);
KeyType target = R[n].key;
printf("查找元素:%d\n", target);
SeqSearch(R, n, target);
BinSearch(R, n, target);
return 0;
}