cpp-algo-cases/chapter_searching/binary_search_insertion.cpp

67 lines
1.9 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: binary_search_insertion.cpp
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><E3A3A8><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD> */
int binarySearchInsertionSimple(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> m
if (nums[m] < target) {
i = m + 1; // target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [m+1, j] <20><>
} else if (nums[m] > target) {
j = m - 1; // target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m-1] <20><>
} else {
return m; // <20>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD> m
}
}
// δ<>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD> i
return i;
}
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD> */
int binarySearchInsertion(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> m
if (nums[m] < target) {
i = m + 1; // target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [m+1, j] <20><>
} else if (nums[m] > target) {
j = m - 1; // target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m-1] <20><>
} else {
j = m - 1; // <20>׸<EFBFBD>С<EFBFBD><D0A1> target <20><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m-1] <20><>
}
}
// <20><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD> i
return i;
}
/* Driver Code */
int main() {
// <20><><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums = ";
printVector(nums);
// <20><><EFBFBD>ֲ<EFBFBD><D6B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>
for (int target : {6, 9}) {
int index = binarySearchInsertionSimple(nums, target);
cout << "Ԫ<EFBFBD><EFBFBD> " << target << " <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << index << endl;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums = ";
printVector(nums);
// <20><><EFBFBD>ֲ<EFBFBD><D6B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>
for (int target : {2, 6, 20}) {
int index = binarySearchInsertion(nums, target);
cout << "Ԫ<EFBFBD><EFBFBD> " << target << " <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << index << endl;
}
return 0;
}