// BinarySearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int BinarySearch(int a[],int s,int e,int value)
{

    int min =s;
    int max = e;
    while (min<max)
    {
        int mid = min+((max-min)/2);
        if(a[mid]<value)
        {
            min=mid;
        }
        if(a[mid]>value)
        {
            max =mid;
        }
        if (a[mid] == value)
        {
            return mid;
        }

    }
    return -1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int a[]={2,4,6,9,10,35,66};
    int t=BinarySearch(a,0,sizeof(a)/sizeof(int),9);
    cout<<t<<endl;

    cin.get();
    return 0;
}

posted on 2010-07-21 20:24  gracestoney  阅读(81)  评论(0编辑  收藏  举报