Files
hello-algo/ja/codes/python/chapter_searching/binary_search_insertion.py
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

54 lines
2.1 KiB
Python

"""
File: binary_search_insertion.py
Created Time: 2023-08-04
Author: krahets (krahets@163.com)
"""
def binary_search_insertion_simple(nums: list[int], target: int) -> int:
"""挿入位置の二分探索(重複要素なし)"""
i, j = 0, len(nums) - 1 # 両端閉区間 [0, n-1] を初期化
while i <= j:
m = i + (j - i) // 2 # 中点インデックス m を計算
if nums[m] < target:
i = m + 1 # ターゲットは区間 [m+1, j] にある
elif nums[m] > target:
j = m - 1 # ターゲットは区間 [i, m-1] にある
else:
return m # ターゲットが見つかった場合、挿入位置 m を返す
# ターゲットが見つからなかった場合、挿入位置 i を返す
return i
def binary_search_insertion(nums: list[int], target: int) -> int:
"""挿入位置の二分探索(重複要素あり)"""
i, j = 0, len(nums) - 1 # 両端閉区間 [0, n-1] を初期化
while i <= j:
m = i + (j - i) // 2 # 中点インデックス m を計算
if nums[m] < target:
i = m + 1 # ターゲットは区間 [m+1, j] にある
elif nums[m] > target:
j = m - 1 # ターゲットは区間 [i, m-1] にある
else:
j = m - 1 # ターゲット未満の最初の要素は区間 [i, m-1] にある
# 挿入位置 i を返す
return i
"""ドライバーコード"""
if __name__ == "__main__":
# 重複要素のない配列
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
print(f"\n配列 nums = {nums}")
# 挿入位置の二分探索
for target in [6, 9]:
index = binary_search_insertion_simple(nums, target)
print(f"要素 {target} の挿入位置インデックスは {index}")
# 重複要素のある配列
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
print(f"\n配列 nums = {nums}")
# 挿入位置の二分探索
for target in [2, 6, 20]:
index = binary_search_insertion(nums, target)
print(f"要素 {target} の挿入位置インデックスは {index}")