c的二分查找函数bsearch

#include <ubase.h>
#include "My850Page.h"
#include "stdlib.h"  //注意这里
#include "iostream.h"
#include "iomanip.h"

/*
void*  bsearch (
 const void * key, //待查找单元的关键字
 const void * base,  //在什么地方找
 size_t num,   //有多少单元可以查找
 size_t width,   //每个单元占多少字节
 int (*fncompare)(const void *, const void * )  //怎样比较两个元素大小
 );
 */

#define MAX 100

void hello(void);


int cmp( const void *a , const void *b )
{
 int x = *(int*)a;
 int y = *(int*)b;
 return x - y;
}

void hello(void)
{
 // TODO: Add code here ...
 
 CMy850Page *page = new CMy850Page;
 page->Create(NULL, "Hello world");
 page->DoModal();
 delete page;
}

int main(void)
{
 //uBaseSetLang(UBASE_LANG_CHS); //GB2312
// uBaseSetLang(UBASE_LANG_CHT); //BIG5
// uBaseSetLang(UBASE_LANG_ENG); //ENGLISH

 //hello();
 // TODO: Add code here ...

 int map[MAX];
 int n = 10;
 int key;
 int *item;
 int i;

 for( i = 0 ; i < n ; i++ )
 {
 map[i] = i * i + 3 * i + 4;
 }

 //cout<<""<<endl;
 printf("数列:/n");
 for( i = 0 ; i < n ; i++ )
 {
  printf("%5d",map[i]);
 }
 //cout<<setw(5)<<map[i];
 //cout<<endl;
 printf("/n");

 key = 44;
 item = (int*)bsearch( &key , map , n , sizeof(int) , cmp );

/*
void*  bsearch (
 const void * key, //待查找单元的关键字
 const void * base,  //在什么地方找
 size_t num,   //有多少单元可以查找
 size_t width,   //每个单元占多少字节
 int (*fncompare)(const void *, const void * )  //怎样比较两个元素大小
 );
 */

 if( item != NULL )
 {
  //cout<<"指定数 "<<*item<<" 在数列中"<<endl;
  printf("指定数 %d 在数列中/n",*item);
 }
 else
 {
  //cout<<"找不到指定数 "<<*item<<endl;
  printf("找不到指定数 %d/n",*item);
 }

 


 USleep(30000);

 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中的二分查找函数是stdlib.h中的bsearch()函数。bsearch()函数用于在一个已排序的数组中查找某个值。它的原型如下: void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); 其中,key是要查找的值,base是指向数组的指针,nmemb是数组中元素的个数,size是数组中每个元素的大小,compar是用于比较两个元素的函数指针。 bsearch()函数返回一个指向找到的元素的指针,如果找不到则返回NULL。要使用该函数,需要提供一个符合要求的比较函数,用于比较数组中的元素和要查找的值。 以下是一个使用bsearch()函数进行二分查找的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 比较函数,用于比较两个整数 int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int size = sizeof(arr) / sizeof(arr<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C语言使用stdlib.h库函数二分查找和快速排序的实现代码](https://download.csdn.net/download/weixin_38637764/12813414)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C语言C语言实现二分查找(折半查找)](https://blog.csdn.net/weixin_53747462/article/details/126923249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值