【算法】二分查找
template <typename T> int binarySearch(T arr[],int n, T targrt){ int l = 0, r = n-1; // arr[l ... r] while(l <= r){ //int mid = (l+r)/2; int mid = l + (r-l)/2; if(arr[mid] == targrt) return mid; if(targrt < arr[mid]) r = mid - 1; else l = mid + 1; } return -1; }
时间复杂度O(log(n));