// BinarySearch.cpp : Defines the entry point for the console application. // Create by Xianyi.Ye, July 25th,2010 #include "stdafx.h" #include <iostream.h> int binarySearch(int num[], int start, int end, int value) { if(start>end) return -1 ; else { int mid = (start+end)/2 ; if(value==num[mid]) return mid ; if(value<num[mid]) return binarySearch(num, start, mid-1, value) ; else return binarySearch(num, mid+1, end, value) ; } } int main(int argc, char* argv[]) { int num[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13} ; int pos = binarySearch(num, 0, 12, 7) ; cout<<"pos="<<pos<<" num="<<num[pos]<<endl ; return 0; }