//
// 二分搜索.c
//
// 说明:本质是二分法求根。
//
// Created by 欧 长坤 on 13-11-05.
// Copyright (c) 2013年 欧 长坤. All rights reserved.
//
#include<stdio.h>intbinary_search(intarray[],intleft,intright,intkey){intmiddle;while(left<=right){middle=(left+right)/2;if(array[middle]==key)returnmiddle;// position
elseif(array[middle]>key)right=middle-1;elseleft=middle+1;}return-1;// not find
}intmain(){intarray[10]={1,2,3,4,5,6,7,8,9,10};intret=binary_search(array,0,10,1);if(ret!=-1)printf("position is %d\n",ret);elseprintf("can not find\n");}