9.3 折半查找
来源:互联网 发布:中银淘宝校园卡办理 编辑:程序博客网 时间:2024/06/10 19:21
折半查找(Binary Searching) 又称为对分查找(二分查找),是在查找表有序(升序或降序排列)的情况下进行的静态查找(即无添加、删除);
其查找过程是:先确定待查记录所在的范围,然后逐步缩小范围直到找到或找不到该记录为止。
注意点:折半查找的效率比顺序查找的高,但折半查找只适用于有序表,其限于顺序存储结构(数组形式存储),对线性链表无法有效的进行折半查找。
折半查找时间复杂度:
最佳状态复杂度为:B(n) = 1 ∈ O(n),
最坏的情况是:W(n) = n ∈ O(logn)
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
/***********************************************************/
// 程序名称:BinarySearch.cpp
// 程序目的:设计一个线性查找的程序
// 程序来源:数据结构与算法分析(C语言描述) P-282
// 日期: 2013-8-28 8:46:03 JohnnyHu改进
/***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
#define NotFound -1
typedef int ElementType;
ElementType data[MAX] = { 12, 14, 19, 22, 25,
32, 39, 40, 45, 47,
48, 53, 54, 59, 60,
68, 69, 68, 70, 77 }; // 数据数组
int BinarySerach(const ElementType a[], ElementType x, int n);
// 主程序
int main(void)
{
int keyValue;
printf("请输入您要查找的(int)值,输入0退出: ");
while(scanf("%d", &keyValue))
{
if (0 == keyValue)
break;
int index = BinarySerach(data, keyValue, MAX);
if (NotFound != index)
printf("您要查找的值是: data[%d] = %d \n", index, data[index]);
else
printf("没找到!\n");
printf("请输入您要查找的(int)值,输入0退出: ");
}
return 0;
}
// 折半查找
int BinarySerach(const ElementType a[], ElementType x, int n)
{
int low, mid, high;
low = 0;
high = n -1;
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] < x)
low = mid + 1;
else if (a[mid] > x)
high = mid - 1;
else
return mid; // 找到
}
return NotFound; // 未找到
}
// 程序名称:BinarySearch.cpp
// 程序目的:设计一个线性查找的程序
// 程序来源:数据结构与算法分析(C语言描述) P-282
// 日期: 2013-8-28 8:46:03 JohnnyHu改进
/***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
#define NotFound -1
typedef int ElementType;
ElementType data[MAX] = { 12, 14, 19, 22, 25,
32, 39, 40, 45, 47,
48, 53, 54, 59, 60,
68, 69, 68, 70, 77 }; // 数据数组
int BinarySerach(const ElementType a[], ElementType x, int n);
// 主程序
int main(void)
{
int keyValue;
printf("请输入您要查找的(int)值,输入0退出: ");
while(scanf("%d", &keyValue))
{
if (0 == keyValue)
break;
int index = BinarySerach(data, keyValue, MAX);
if (NotFound != index)
printf("您要查找的值是: data[%d] = %d \n", index, data[index]);
else
printf("没找到!\n");
printf("请输入您要查找的(int)值,输入0退出: ");
}
return 0;
}
// 折半查找
int BinarySerach(const ElementType a[], ElementType x, int n)
{
int low, mid, high;
low = 0;
high = n -1;
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] < x)
low = mid + 1;
else if (a[mid] > x)
high = mid - 1;
else
return mid; // 找到
}
return NotFound; // 未找到
}
输出结果:
2、 在递归情况的下的折半查找:
修改函数:int BinarySerach(const ElementType a[], ElementType x, int n);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 折半查找(递归)
int BinarySerach(int low, int high, int keyvalue)
{
int mid = -1;
if (low > high)
return NotFound;
else
{
mid = (low + high) / 2;
if (data[mid] < keyvalue)
return BinarySerach(mid+1, high, keyvalue);
else if (data[mid] > keyvalue)
return BinarySerach(low, mid-1, keyvalue);
else
return mid;
}
return NotFound; // 未找到
}
int BinarySerach(int low, int high, int keyvalue)
{
int mid = -1;
if (low > high)
return NotFound;
else
{
mid = (low + high) / 2;
if (data[mid] < keyvalue)
return BinarySerach(mid+1, high, keyvalue);
else if (data[mid] > keyvalue)
return BinarySerach(low, mid-1, keyvalue);
else
return mid;
}
return NotFound; // 未找到
}
在主函数中
int index = BinarySerach(data, keyValue, MAX);
修改为
int index = BinarySerach(0, MAX-1, keyValue);
- 9.3 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- hdu4415 Assassin’s Creed
- 赖勇浩:推荐《Linux 多线程服务器端编程》
- “啃老”族,蹭住多久才算久?
- hdu4414 Finding crosses
- [二次开发]dede会员列表调用(企业、个人)
- 9.3 折半查找
- clip:rect('top', 'right', 'bottom', 'left')
- Jackson多态类型数据的反序列化
- hdu4417 Super Mario
- QT 多页面切换之QTabWidget
- 20130827组队赛Regionals 2012, North America - Greater NY
- 使用C/C++扩展Python zz
- 图像二值化算法
- 大把免费编程书籍