C++ 查找算法
- 作者:xiaoxiao
- 发表时间:2020-12-23 10:40
- 来源:未知
int SeqSearch(int arr[], int len, int target) { ??? int i = 0; ??? while ((i != len) && (arr[i] != target)) ??? { ??????? i++; ??? } ??? if (i < len) ??? { ??????? return i; ??? } ??? else ??? { ??????? return -1; ??? } }
int BinSearch(int arr[], int len, int target){??? int low = 0;??? int high = len-1;??? while (low <= high)??? {??????? int mid = (low + high) / 2;??????? if (arr[mid] == target)??????? {??????????? return mid;??????? }??????? else if (arr[mid] > target)??????? {??????????? high = mid - 1;??????? }??????? else??????? {??????????? low = mid + 1;??????? }??? }??? return -1;}