前几天参加2014阿里巴巴春季校招(测试开发岗)笔试, 里面有一道改错题, 就是给了一个binary_search的代码. 结果这么基础的一题我还是没能答全对. 悲剧悲剧, 这个水平注定去不了阿里...现在开始亡羊补牢.
算法: binarySearch
输入: 数组a, 起始索引begin, 结束索引end, 所查找值value
输出: 若找到则返回value在a中索引, 否则返回-1.
后面两个版本的二分法都能通过以下用例:
0. a == NULL
1. 边界值, 即begin和end
2. 最后会剩两个数min, max. 查找key, key == min
3. 最后会剩两个数min, max. 查找max, key == max
4. 最后会剩两个数min, max. 查找key, key < min
5. 最后会剩两个数min, max. 查找key, min < key < max
6. 最后会剩两个数min, max. 查找key, key > max
如果有高人发现不能通过的用例还求不吝赐教. 跪谢.
自己先写一个:
int binarySearch(int* a, int begin, int end, int value){ if(!a) return -1; int min = begin; int max = end; int middle; while(min <= max){ middle = min + (max - min) / 2; //防止溢出,不写成mid = (max+min)/2 if(a[middle] == value) return middle; else if(a[middle] < value){ min = middle + 1; } else { max = middle - 1; } } return -1; }
int binarySearchBoP(int* arr, int begin, int end, int val){ if(NULL == arr) return -1; int minIndex = begin; int maxIndex = end; int middleIndex; while(minIndex < maxIndex - 1){ middleIndex = minIndex + (maxIndex - minIndex)/2; //防止溢出,不写成mid = (max+min)/2 if(arr[middleIndex] <= val){ minIndex = middleIndex; } else { maxIndex = middleIndex; } } if(arr[minIndex] == val){ return minIndex; } else if(arr[maxIndex] == val){ return maxIndex; } else { return -1; } }
#include <iostream> #include <cstdlib> using namespace std; typedef int (*BSFuncPointer)(int*, int, int, int); int binarySearch(int* a, int begin, int end, int value){ if(!a) return -1; int min = begin; int max = end; int middle; while(min <= max){ middle = min + (max - min) / 2; if(a[middle] == value) return middle; else if(a[middle] < value){ min = middle + 1; } else { max = middle - 1; } } return -1; } int binarySearchBoP(int* arr, int begin, int end, int val){ if(NULL == arr) return -1; int minIndex = begin; int maxIndex = end; int middleIndex; while(minIndex < maxIndex - 1){ middleIndex = minIndex + (maxIndex - minIndex)/2; if(arr[middleIndex] <= val){ minIndex = middleIndex; } else { maxIndex = middleIndex; } } if(arr[minIndex] == val){ return minIndex; } else if(arr[maxIndex] == val){ return maxIndex; } else { return -1; } } void test_binarySearch(BSFuncPointer binarySearch, int* a, int length, int value, int expectedResult){ if (binarySearch(a, 0, length-1, value) == expectedResult){ cout << "Test passed!" << endl; } else { cout << "Test failed with parameters[a, length, value, expectedResult]: " << a << ',' << length << ',' << value << ',' << expectedResult << endl; } } void test(BSFuncPointer func, const char* funcName, int* arr, int length){ cout << "Test func: " << funcName << endl; // case0 test_binarySearch(func, NULL, length, 0, -1); // case1 test_binarySearch(func, arr, length, 1, 0); test_binarySearch(func, arr, length, 21, 10); // case2 test_binarySearch(func, arr, length, 7, 3); // case3 test_binarySearch(func, arr, length, 3, 1); // case4 test_binarySearch(func, arr, length, 0, -1); // case5 test_binarySearch(func, arr, length, 2, -1); // case6 test_binarySearch(func, arr, length, 4, -1); } int main(){ int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}; // 11个元素 int length = sizeof(arr) / sizeof(int); test(binarySearch, "binarySearch", arr, length); test(binarySearchBoP, "binarySearchBoP", arr, length); system("pause"); return 0; }