标签:
#include <stdio.h> int searchItem(int a[],int len,int key){ //定义变量 int low = 0; //数组的长度-1 int high = len-1; int mid = -1; //通过循环查找 while (low<=high) { //计算中间位置 mid = (low + high)/2; //int //到右半侧去查找 if (key > a[mid]) { //改变low的指向 low = mid + 1; }else if (key < a[mid]){ high = mid - 1; }else{ return mid; } } return -1; } int main(int argc, const char * argv[]) { // 0 1 2 3 4 5 6 7 8 9 int a[10]={12,23,33,42,45,67,77,82,92,100}; int loc = searchItem(a, 10,11); printf("%d\n",loc); return 0; }
标签:
原文地址:http://www.cnblogs.com/iospp/p/5074598.html