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

58 lines
2.5 KiB
Java
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.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search {
/* 二分探索(両端閉区間) */
static int binarySearch(int[] nums, int target) {
// 両端閉区間 [0, n-1] を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素を指す
int i = 0, j = nums.length - 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;
}
/* 二分探索(左閉右開区間) */
static int binarySearchLCRO(int[] nums, int target) {
// 左閉右開区間 [0, n) を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素+1を指す
int i = 0, j = nums.length;
// 探索区間が空になるまでループ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;
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分探索(両端閉区間) */
int index = binarySearch(nums, target);
System.out.println("目標要素 6 のインデックス = " + index);
/* 二分探索(左閉右開区間) */
index = binarySearchLCRO(nums, target);
System.out.println("目標要素 6 のインデックス = " + index);
}
}