Browse Source

上传文件至 'Algorithm'

算法模块上传
ZZWX 1 year ago
parent
commit
c3178ed0f6
1 changed files with 14 additions and 0 deletions
  1. 14 0
      Algorithm/自定义二叉查找模板.cpp

+ 14 - 0
Algorithm/自定义二叉查找模板.cpp

@@ -0,0 +1,14 @@
+//cmp(compare function):from front to rear
+template<typename _T>
+int BinarySearchInc(_T* x, _T target, bool* cmp(_T, _T), int start = 0, int end) {
+	int L = start, R = end, M, res = -1;
+	while (L <= R) {
+		M = (L + R) >> 1;
+		if (cmp(x[M], target))
+			res = M,
+			R = M - 1;
+		else
+			L = M + 1;
+	}
+	return res;
+}