Files
hello-algo/ja/codes/cpp/chapter_searching/binary_search_insertion.cpp
Ikko Eltociear Ashimine 954c45864b docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
2025-10-17 05:04:43 +08:00

66 lines
2.4 KiB
C++

/**
* File: binary_search_insertion.cpp
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 挿入ポイントの二分探索(重複要素なし) */
int binarySearchInsertionSimple(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 両端閉区間[0, n-1]を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックスmを計算
if (nums[m] < target) {
i = m + 1; // ターゲットは区間[m+1, j]にある
} else if (nums[m] > target) {
j = m - 1; // ターゲットは区間[i, m-1]にある
} else {
return m; // ターゲットが見つかったため、挿入ポイントmを返す
}
}
// ターゲットが見つからなかったため、挿入ポイントiを返す
return i;
}
/* 挿入ポイントの二分探索(重複要素あり) */
int binarySearchInsertion(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // 両端閉区間[0, n-1]を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックスmを計算
if (nums[m] < target) {
i = m + 1; // ターゲットは区間[m+1, j]にある
} else if (nums[m] > target) {
j = m - 1; // ターゲットは区間[i, m-1]にある
} else {
j = m - 1; // ターゲット未満の最初の要素は区間[i, m-1]にある
}
}
// 挿入ポイントiを返す
return i;
}
/* ドライバコード */
int main() {
// 重複要素のない配列
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
cout << "\n配列 nums = ";
printVector(nums);
// 挿入ポイントの二分探索
for (int target : {6, 9}) {
int index = binarySearchInsertionSimple(nums, target);
cout << "要素 " << target << " の挿入ポイントインデックスは " << index << " です" << endl;
}
// 重複要素を含む配列
nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\n配列 nums = ";
printVector(nums);
// 挿入ポイントの二分探索
for (int target : {2, 6, 20}) {
int index = binarySearchInsertion(nums, target);
cout << "要素 " << target << " の挿入ポイントインデックスは " << index << " です" << endl;
}
return 0;
}