Files
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

59 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: binary_search.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* 二分探索(両端閉区間) */
int binarySearch(vector<int> &nums, int target) {
// 両端閉区間[0, n-1]を初期化、すなわちi、jはそれぞれ配列の最初の要素と最後の要素を指す
int i = 0, j = nums.size() - 1;
// 探索区間が空になるまでループi > jの時空になる
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックスmを計算
if (nums[m] < target) // この状況はtargetが区間[m+1, j]にあることを示す
i = m + 1;
else if (nums[m] > target) // この状況はtargetが区間[i, m-1]にあることを示す
j = m - 1;
else // ターゲット要素が見つかったため、そのインデックスを返す
return m;
}
// ターゲット要素が見つからなかったため、-1を返す
return -1;
}
/* 二分探索(左閉右開区間) */
int binarySearchLCRO(vector<int> &nums, int target) {
// 左閉右開区間[0, n)を初期化、すなわちi、jはそれぞれ配列の最初の要素と最後の要素+1を指す
int i = 0, j = nums.size();
// 探索区間が空になるまでループi = jの時空になる
while (i < j) {
int m = i + (j - i) / 2; // 中点インデックスmを計算
if (nums[m] < target) // この状況はtargetが区間[m+1, j)にあることを示す
i = m + 1;
else if (nums[m] > target) // この状況はtargetが区間[i, m)にあることを示す
j = m;
else // ターゲット要素が見つかったため、そのインデックスを返す
return m;
}
// ターゲット要素が見つからなかったため、-1を返す
return -1;
}
/* ドライバコード */
int main() {
int target = 6;
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
/* 二分探索(両端閉区間) */
int index = binarySearch(nums, target);
cout << "ターゲット要素6のインデックス =" << index << endl;
/* 二分探索(左閉右開区間) */
index = binarySearchLCRO(nums, target);
cout << "ターゲット要素6のインデックス =" << index << endl;
return 0;
}