zoukankan      html  css  js  c++  java
  • bsearch

    简介

      函数名: bsearch

      功 能: 二分法搜索

      用 法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *));

      语法:

      #include <stdlib.h>

            void *bsearch( const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *,        const void *) );

      功能: 函数用折半查找法在从数组元素buf[0]到buf[num-1] 匹配参数key。如果函数compare 的第一个参数小于第二个参数,返回负值;如果等于返回零值;如果大于返回正值。数组buf 中的元素应以升序排列。函数bsearch()的返回值是指向匹配项,如果没有发现匹配项,返回NULL

    程序例

      程序例:

      #include<stdlib.h>


      #include<stdio.h>


      #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))


      int numarray[] = {123, 145, 512, 627, 800, 933};


      int numeric (const int *p1, const int *p2){


      return(*p1 - *p2);


      }


      int lookup(int key){


      int *itemptr;


      /* The cast of (int(*)(const void *,const void*))


      is needed to avoid a type mismatch error at


      compile time
    */


      itemptr = (int *)bsearch (&amp;key, numarray, NELEMS(numarray),


      sizeof(int), (int(*)(const void *,const void *))numeric);


      return (itemptr != NULL);


      }


      int main(void){


      if (lookup(512)){


      printf("512 is in the table.\n");


      }


      else{


      printf("512 isn't in the table.\n");


      }


      return 0;


      }


      例2:


      对一个字符数组进行排序并查找指定字符串的例子:


      #include <stdlib.h>


      #include <stdio.h>


      #include <string.h>


      #define LENGTH(x) sizeof(x)/sizeof(x[0])


      /**输出数组元素


      *\param arr:指向数组的指针


      *\param len:数组元素的个数


      
    */


      void print(char (*arr)[10],int len)


      {


      int i;


      for (i=0;i<len;i++)


      {


      printf("%s ",arr[i]);


      }


      printf("\n");


      }


      int main()


      {


      char arr[][10]={"bac","bca","abc","acb","cba","cab"}; /* 定义二维字符数组*/


      char *key="bca";/* 要查找的字符串*/


      char *ptr=NULL; /* 字符指针*/


      /* 输出未排序时字符数组的内容*/


      printf("before qsort :");


      print(arr,LENGTH(arr));


      /* 使用qsort对字符数组排序*/


      qsort((void *)arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);


      /* 输出排序后字符数组的内容*/


      printf("after qsort :");


      print(arr,LENGTH(arr));


      /* 采用二分查找查找指定字符*/


      ptr=(char *)bsearch(key,arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);


      if (ptr)


      { /* 找到*/


      printf("%s is in the array\n",key);


      }


      else/* 没找到*/


      {


      printf("%s isn't in the array\n",key);


      }


      return 0;


      }
    作者:BuildNewApp
    出处:http://syxchina.cnblogs.comBuildNewApp.com
    本文版权归作者、博客园和百度空间共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则作者会诅咒你的。
    如果您阅读了我的文章并觉得有价值请点击此处,谢谢您的肯定1。
  • 相关阅读:
    Linux 安装 iptables防火墙
    CentOS最常用命令及快捷键整理
    WebAPI 和 webservice接口
    Linux 文件权限
    Linux查看系统信息的一些命令及查看已安装软件包的命令
    navicat连接虚拟机(centos)中的mysql
    Nmap扫描与Tcpdump抓包分析
    python 识别验证码自动登陆
    iptables开通某些端口
    hive的安装和使用
  • 原文地址:https://www.cnblogs.com/syxchina/p/2197378.html
Copyright © 2011-2022 走看看