C/C++折半查找函数bsearch
来源:互联网 发布:linux网卡设置命令 编辑:程序博客网 时间:2024/06/07 05:02
头文件<stdlib.h> <cstdlib>
1. 函数原型
void * bsearch(const void * key, const void * base, size_t num, size_t size, int (*comparator) ( const void *, const void * ) );
2. 函数说明
Binary search in array:
Searches the given key in the array pointed by base that is formed by num elements, each of a size of size bytes, and returns a void* pointer to the first entry in the table that matches the search key.
To perform the search, the function compares the elements to the key by calling the function comparator specified as the last argument.
Because this function performs a binary search, the values in the base array are expected to be already sorted in ascending order, with the same criteria used by comparator.
3. 函数参数
key
Pointer to the object that serves as key for the search, type-casted as a void*.
base
Pointer to the first object of the array where the search is performed, type-casted as a void*.
num
Number of elements in the array pointed by base.
size
Size in bytes of each element in the array.
comparator
Function that compares two elements. The function shall follow this prototype:
int comparator ( const void * pkey, const void * pelem );
The function must accept two parameters: the first one pointing to the key object, and the second one to an
element of the array, both type-casted as void*. The function should cast the parameters back to some data
type and compare them.
The return value of this function should represent whether the value pointed by pkey is considered less than,
equal, or grater than the value pointed by pelem by returning, respectively, a negative value, zero or a positive
value.
For types that support regular comparison operators, a general comparator function may look like:
int compareMyType (const void * a, const void * b) { if ( *(MyType*)a > *(MyType*)b ) return 1; if ( *(MyType*)a == *(MyType*)b ) return 0; if ( *(MyType*)a < *(MyType*)b ) return -1; }
4. 返回类型
A pointer to an entry in the array that matches the search key.
If key is not found, a NULL pointer is returned.
5. 示例
/* bsearch example */#include <stdio.h>#include <stdlib.h>int compareints (const void * a, const void * b){ return ( *(int*)a - *(int*)b );}int values[] = { 10, 20, 25, 40, 90, 100 };int main (){ int * pItem; int key = 40; pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints); if (pItem!=NULL) printf ("%d is in the array.\n",*pItem); else printf ("%d is not in the array.\n",key); return 0;}
In the example there is an array of sorted int values. There is also a function called compare that compares the values pointed by the two parameters as if they were pointers to int values (which they indeed are) and returns the result of the subtraction of the values pointed by both, which gives 0 as result if they are equal, a positive result if the value pointed by a is greater than the pointed by b or a negative result if the value pointed by b is greater.
In the main, function there is a call to bsearch with 40 as key, so the function finds that key in the array of values and the program prints out:
For C strings, strcmp can directly be used as the last argument for bsearch:
/* bsearch example with strings */#include <stdio.h>#include <stdlib.h>#include <string.h>char strvalues[][20] = {"some","example","strings","here"};int main (){ char * pItem; char key[20] = "example"; /* sort elements in array: */ qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp); /* search for the key: */ pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp); if (pItem!=NULL) printf ("%s is in the array.\n",pItem); else printf ("%s is not in the array.\n",key); return 0;}
输出:
- C/C++折半查找函数bsearch
- C语言中库函数自带的查找函数bsearch
- 快速排序与折半查找算法函数:qsort与bsearch
- 折半查找的函数实现(C++)
- 用c语言实现折半查找函数
- c语言:编写折半查找函数
- 【C语言】编写一个折半查找函数
- C语言:编写折半查找函数
- bsearch.c
- C语言中用bsearch()实现查找操作
- C/C++ 库函数 二分查找 bsearch
- 二分查找bsearch.c源码分析
- C 语言中用bsearch()实现查找操作
- C 语言中用bsearch()实现查找操作
- 折半查找 c
- C语言 -- 折半查找
- C语言 折半查找
- C语言 折半查找
- c++中的string常用函数用法
- 如何成为一个编程高手
- poj2553--Tarjan
- .NET 中 操作excel 系列--copy功能.
- 用highcharts动态获取值,以及x轴值可以改变
- C/C++折半查找函数bsearch
- How to: Load Unmanaged Resources into a Byte Array
- GPGGA数据解析
- js保持div悬浮,不随页面向下滚动而被覆盖
- linux 系统博客
- 【DP】 hdu3237 Help Bubu
- Psiphon 3
- 数据库的部分成操作
- 锁定屏幕输入框