首页 > 其他 > 详细

二分查找算法

时间:2014-08-20 12:41:22      阅读:322      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
int BinSearch(int Source[],int size,int key)
{
    int low=0, high=size-1,mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(Source[mid] == key)
            return mid;
        if(Source[mid] >  key)
            high=mid-1;
        else
            low=mid+1;
    }
    return -1;
}

void main()
{
    int num;
    int index;
    int ArraySource[10]={1,2,8,11,12,13,14,15,16,17};//Array after sorting
    printf("Plese input the number to find\n");
    scanf("%d",&num);
    index=BinSearch(ArraySource,10,num);
    if(index>=0)
        printf("The number you are finding locates @ [%d]\n",index+1);
    else
        printf("The number is not in source array \n");
}


二分查找算法,布布扣,bubuko.com

二分查找算法

原文:http://my.oschina.net/lvyi/blog/304628

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!