ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

二分查找(递归与非递归)

2020-07-30 19:32:09  阅读:29  来源: 互联网

标签:二分 递归 int list mid high length 查找


#include <stdlib.h>
#include <stdio.h>
//二分查找非递归

int Binary_Search(int list[],int key,int length){
    int low=0,high=length-1;
    while (low<=high){
        int mid=(high+low)/2;
        if (list[mid]==key)
            return ++mid;
        else if (list[mid]>key)
            high=mid-1;
        else if (list[mid]<key)
            low=mid+1;
    }
    return -1;
}
int main(){
    int list[10]={1,3,5,6,7,8,9,10,11,19};
    int number=Binary_Search(list,10,10);
    printf("%d",number);
    return 0;
}

标签:二分,递归,int,list,mid,high,length,查找
来源: https://www.cnblogs.com/szj666/p/13405912.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有