main.c
- #include <stdio.h>
- #include "findbyhalf.h"
- int main(int argc, const char * argv[]) {
- int arr[]={11,22,33,44,55,66,77,88,99,100};
- int location=locationSearch(arr, 10, 12);
- printf("%d\n",location);
- return 0;
- }
复制代码
findbyhalf.c
- #include "findbyhalf.h"
- int locationSearch(int arr[],int len,int goal)
- {
- int low=0,high=len-1,mid;
- while (low<=high)
- {
- mid=(low+high)/2;
- if (goal>arr[mid]){
- low=mid+1;
- }else if(goal<arr[mid]){
- high=mid-1;
- }else{
- return mid;
- }
- }
- return -1;
- }
复制代码 |
|