//二分法查找int BinarySearch(){ int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12}; int key=3; int head=0, tail=11, middle; while (tail>head) { middle = (head + tail) / 2; if (key > arr[middle]) head = middle + 1; else if (key < arr[middle]) tail = middle - 1; else return 1; } return -1;}//二分法查找 递归int Binary_Search_By_Recursive(int arr[],int low,int high,int key){ if (low > high) { return -1; } int middle = (low + high) / 2; if (key < *(arr+middle)) Binary_Search_By_Recursive(arr,low,middle-1,key); else if (key > *(arr+middle)) Binary_Search_By_Recursive(arr, middle + 1, high, key); else return 1;}//main函数void main(){ int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; //Two_dimensional_array(); //Tow_dimensinal_array_init(); //Tow_dimensinal_array_init_oneFor(); //There_dimensinal_array_init(); //There_dimensinal_array_init_oneFor(); //int flag= BinarySearch(); int flag = Binary_Search_By_Recursive(arr,0,11,-1); printf("%d ",flag); getchar();}
版权声明:本文为博主原创文章,未经博主允许不得转载。