C语言二分查找函数

int search(int* nums, int numsSize, int target) {
    int low,
        high,
        mid;
    low = 0;
    high = numsSize - 1;
    while (low <= high) {
        mid = (low + high) / 2;
        if(target == nums[mid]) {
            return mid;
        }
        if(target < nums[mid]) {
            high = mid - 1;
        }else{
            low = mid + 1;
        }
    }
    return -1;
}

主要思路:1.定义初始数组的首尾两端的索引;2.获取数组中间的索引;3.判断与目标值的大小;

C函数库手册,按照函数功能来分类 分类函数,所在函数库为ctype.h int isalpha(int ch) 若ch 是字母('A'-'Z','a'-'z')返回非0 值,否则返回0 int isalnum(int ch) 若ch 是字母('A'-'Z','a'-'z')或数字('0'-'9')返回非0 值,否则返回0 ...... 数学函数,所在函数库为math.h、stdlib.h、string.h、float.h int abs(int i) 返回整型参数i 的绝对值 double cabs(struct complex znum) 返回复数znum 的绝对值 double fabs(double x) 返回双精度参数x 的绝对值 ...... 目录函数,所在函数库为dir.h、dos.h int chdir(char *path) 使指定的目录path(如:"C:\\WPS")变成当前的工作目录,成 功返回0 int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功 返回0 ...... 进程函数,所在函数库为stdlib.h、process.h void abort() 此函数通过调用具有出口代码3 的_exit 写一个终止信息于stderr,并异常终止程序。无返回值 int exec…装入和运行其它程序 ...... 转换子程序,函数库为math.h、stdlib.h、ctype.h、float.h char *ecvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 char *fcvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 ...... 诊断函数,所在函数库为assert.h、math.h void assert(int test) 一个扩展成if 语句那样的宏,如果test 测试失败,就显示一个信息并异常终止程序,无返回值 void perror(char *string) 本函数将显示最近一次的错误信息,格式如下:字符串string:错误信息 ...... 输入输出子程序,函数库为io.h、conio.h、stat.h、dos.h、stdio.h、signal.h int kbhit() 本函数返回最近所敲的按键 int fgetchar() 从控制台(键盘)读一个字符,显示在屏幕上 ...... 接口子程序,所在函数库为:dos.h、bios.h unsigned sleep(unsigned seconds)暂停seconds 微秒(百分之一秒) int unlink(char *filename)删除文件filename unsigned FP_OFF(void far *farptr)本函数用来取远指针farptr 的偏移量 ...... 存贮分配子程序,所在函数库为dos.h、alloc.h、malloc.h、stdlib.h、process.h int allocmem(unsigned size,unsigned *seg)利用DOS 分配空闲的内存,size 为分配内存大小,seg 为分配后的内存指针 int freemem(unsigned seg)释放先前由allocmem 分配的内存,seg 为指定的内存指针 ...... 操作函数,所在函数库为string.h、mem.h mem…操作存贮数组 ...... ......
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值