1#include <stdio.h>2#include <stdlib.h>3 4 intBinsearch (intXintV[],intn);//function Declaration5 6 intMain ()7 {8 intarr[]={1,2,3,4,5,6,7,8,9,Ten};9 intas a result, num;Tennum=7; OneResults =binsearch (Num,arr,Ten); A - if(Results <0) -printf"I didn't find!\n ."); the Else -printf"find%d\n in arr[%d]", as a result, num); -System"Pause"); - + return 0; - } + A intBinsearch (intXintV[],intN//function Definition at { - intLow,high,mid;//min, max, Middle - -low=0; -high=n-1; - in while(Low<=high) {//Keep binary . - toMid= (Low+high)/2; + if(X<v[mid])//If the number you are looking for is smaller than the middle -high=mid-1;//throw half . the Else if(X>v[mid])//if it's bigger than the middle *Low=mid+1;//throw away the other half $ ElsePanax Notoginseng returnMid//Find - the + } A the return-1;//didn't find +}
Binary lookup of algorithms and data structures (C language)