// array为待查找数组 // n为数组array的元素个数 // key为查找的元素 int BinarySearch(int *array, int n, int key) { int head = 0; int tail = n - 1; int middle; while(head <= tail) { middle = (head + tail) / 2; printf("middle = %d ", middle); if(key == array[middle]) { return middle; } if(key > array[middle]) { head = middle + 1; continue; } if(key < array[middle]) { tail = middle - 1; continue; } } return -1; }