Add the section of binary search insertion. (#671)
Refactor the section of binary search edge. Finetune the figures of binary search.
@ -1,59 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.c
 | 
			
		||||
 * Created Time: 2023-05-31
 | 
			
		||||
 * Author: Gonglja (glj0@outlook.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "../utils/common.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
int binarySearchLeftEdge(int *nums, int size, int target) {
 | 
			
		||||
    int i = 0, j = size - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target)
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        else if (nums[m] > target)
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        else
 | 
			
		||||
            j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
    }
 | 
			
		||||
    if (i == size || nums[i] != target)
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
int binarySearchRightEdge(int *nums, int size, int target) {
 | 
			
		||||
    int i = 0, j = size - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target)
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        else if (nums[m] > target)
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        else
 | 
			
		||||
            i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
    }
 | 
			
		||||
    if (j < 0 || nums[j] != target)
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    return j;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
int main() {
 | 
			
		||||
    int target = 6;
 | 
			
		||||
    int nums[] = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
 | 
			
		||||
    int size = sizeof(nums) / sizeof(nums[0]);
 | 
			
		||||
 | 
			
		||||
    // 二分查找最左一个元素
 | 
			
		||||
    int indexLeft = binarySearchLeftEdge(nums, size, target);
 | 
			
		||||
    printf("数组中最左一个元素 6 的索引 = %d\n", indexLeft);
 | 
			
		||||
 | 
			
		||||
    // 二分查找最右一个元素
 | 
			
		||||
    int indexRight = binarySearchRightEdge(nums, size, target);
 | 
			
		||||
    printf("数组中最右一个元素 6 的索引 = %d\n", indexRight);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
@ -1,3 +1,4 @@
 | 
			
		||||
add_executable(binary_search binary_search.cpp)
 | 
			
		||||
add_executable(binary_search_insertion binary_search_insertion.cpp)
 | 
			
		||||
add_executable(binary_search_edge binary_search_edge.cpp)
 | 
			
		||||
add_executable(two_sum two_sum.cpp)
 | 
			
		||||
 | 
			
		||||
@ -1,57 +1,66 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.cpp
 | 
			
		||||
 * Created Time: 2023-05-21
 | 
			
		||||
 * Created Time: 2023-08-04
 | 
			
		||||
 * Author: Krahets (krahets@163.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "../utils/common.hpp"
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
int binarySearchLeftEdge(vector<int> &nums, int target) {
 | 
			
		||||
/* 二分查找插入点(存在重复元素) */
 | 
			
		||||
int binarySearchInsertion(const 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)
 | 
			
		||||
        if (nums[m] < target) {
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        else if (nums[m] > target)
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        else
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    if (i == nums.size() || nums[i] != target)
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    // 返回插入点 i
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
int binarySearchRightEdge(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; // target 在区间 [m+1, j] 中
 | 
			
		||||
        else if (nums[m] > target)
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        else
 | 
			
		||||
            i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
/* 二分查找最左一个 target */
 | 
			
		||||
int binarySearchLeftEdge(vector<int> &nums, int target) {
 | 
			
		||||
    // 等价于查找 target 的插入点
 | 
			
		||||
    int i = binarySearchInsertion(nums, target);
 | 
			
		||||
    // 未找到 target ,返回 -1
 | 
			
		||||
    if (i == nums.size() || nums[i] != target) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    if (j < 0 || nums[j] != target)
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    // 找到 target ,返回索引 i
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个 target */
 | 
			
		||||
int binarySearchRightEdge(vector<int> &nums, int target) {
 | 
			
		||||
    // 转化为查找最左一个 target + 1
 | 
			
		||||
    int i = binarySearchInsertion(nums, target + 1);
 | 
			
		||||
    // j 指向最右一个 target ,i 指向首个大于 target 的元素
 | 
			
		||||
    int j = i - 1;
 | 
			
		||||
    // 未找到 target ,返回 -1
 | 
			
		||||
    if (j == -1 || nums[j] != target) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    // 找到 target ,返回索引 j
 | 
			
		||||
    return j;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
int main() {
 | 
			
		||||
    int target = 6;
 | 
			
		||||
    // 包含重复元素的数组
 | 
			
		||||
    vector<int> nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
 | 
			
		||||
    cout << "\n数组 nums = ";
 | 
			
		||||
    printVector(nums);
 | 
			
		||||
 | 
			
		||||
    // 二分查找最左一个元素
 | 
			
		||||
    int indexLeft = binarySearchLeftEdge(nums, target);
 | 
			
		||||
    cout << "数组中最左一个元素 6 的索引 = " << indexLeft << endl;
 | 
			
		||||
 | 
			
		||||
    // 二分查找最右一个元素
 | 
			
		||||
    int indexRight = binarySearchRightEdge(nums, target);
 | 
			
		||||
    cout << "数组中最右一个元素 6 的索引 = " << indexRight << endl;
 | 
			
		||||
    // 二分查找左边界和右边界
 | 
			
		||||
    for (int target : {6, 7}) {
 | 
			
		||||
        int index = binarySearchLeftEdge(nums, target);
 | 
			
		||||
        cout << "最左一个元素 " << target << " 的索引为 " << index << endl;
 | 
			
		||||
        index = binarySearchRightEdge(nums, target);
 | 
			
		||||
        cout << "最右一个元素 " << target << " 的索引为 " << index << endl;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										66
									
								
								codes/cpp/chapter_searching/binary_search_insertion.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						@ -0,0 +1,66 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.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; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            return m; // 找到 target ,返回插入点 m
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 未找到 target ,返回插入点 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; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 返回插入点 i
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
@ -1,57 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
* File: binary_search_edge.cs
 | 
			
		||||
* Created Time: 2023-06-01
 | 
			
		||||
* Author: hpstory (hpstory1024@163.com)
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
namespace hello_algo.chapter_searching; 
 | 
			
		||||
 | 
			
		||||
public class binary_search_edge {
 | 
			
		||||
    /* 二分查找最左一个元素 */
 | 
			
		||||
    public static int binarySearchLeftEdge(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target)
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            else if (nums[m] > target)
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            else
 | 
			
		||||
                j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
        if (i == nums.Length || nums[i] != target)
 | 
			
		||||
            return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
        return i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* 二分查找最右一个元素 */
 | 
			
		||||
    public static int binarySearchRightEdge(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target)
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            else if (nums[m] > target)
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            else
 | 
			
		||||
                i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        }
 | 
			
		||||
        if (j < 0 || nums[j] != target)
 | 
			
		||||
            return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
        return j;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [Test]
 | 
			
		||||
    public void Test() {
 | 
			
		||||
        int target = 6;
 | 
			
		||||
        int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
 | 
			
		||||
 | 
			
		||||
        // 二分查找最左一个元素
 | 
			
		||||
        int indexLeft = binarySearchLeftEdge(nums, target);
 | 
			
		||||
        Console.WriteLine("数组中最左一个元素 6 的索引 = " + indexLeft);
 | 
			
		||||
 | 
			
		||||
        // 二分查找最右一个元素
 | 
			
		||||
        int indexRight = binarySearchRightEdge(nums, target);
 | 
			
		||||
        Console.WriteLine("数组中最右一个元素 6 的索引 = " + indexRight);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,50 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.dart
 | 
			
		||||
 * Created Time: 2023-06-01
 | 
			
		||||
 * Author: liuyuxin (gvenusleo@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
int binarySearchLeftEdge(List<int> nums, int target) {
 | 
			
		||||
  int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
  while (i <= j) {
 | 
			
		||||
    int m = i + (j - i) ~/ 2; // 计算中间索引 m
 | 
			
		||||
    if (nums[m] < target)
 | 
			
		||||
      i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
    else if (nums[m] > target)
 | 
			
		||||
      j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
    else
 | 
			
		||||
      j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
  }
 | 
			
		||||
  if (i == nums.length || nums[i] != target) return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
  return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
int binarySearchRightEdge(List<int> nums, int target) {
 | 
			
		||||
  int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
  while (i <= j) {
 | 
			
		||||
    int m = i + (j - i) ~/ 2; // 计算中间索引 m
 | 
			
		||||
    if (nums[m] < target)
 | 
			
		||||
      i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
    else if (nums[m] > target)
 | 
			
		||||
      j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
    else
 | 
			
		||||
      i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
  }
 | 
			
		||||
  if (j < 0 || nums[j] != target) return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
  return j;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
void main() {
 | 
			
		||||
  int target = 6;
 | 
			
		||||
  List<int> nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
 | 
			
		||||
 | 
			
		||||
  // 二分查找最左一个元素
 | 
			
		||||
  int indexLeft = binarySearchLeftEdge(nums, target);
 | 
			
		||||
  print("数组中最左一个元素 6 的索引 = $indexLeft");
 | 
			
		||||
  // 二分查找最右一个元素
 | 
			
		||||
  int indexRight = binarySearchRightEdge(nums, target);
 | 
			
		||||
  print("数组中最右一个元素 6 的索引 = $indexRight");
 | 
			
		||||
}
 | 
			
		||||
@ -1,55 +0,0 @@
 | 
			
		||||
// File: binary_search_edge.go
 | 
			
		||||
// Created Time: 2023-05-29
 | 
			
		||||
// Author: Reanon (793584285@qq.com)
 | 
			
		||||
 | 
			
		||||
package chapter_searching
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
func binarySearchLeftEdge(nums []int, target int) int {
 | 
			
		||||
	// 初始化双闭区间 [0, n-1]
 | 
			
		||||
	i, j := 0, len(nums)-1
 | 
			
		||||
	for i <= j {
 | 
			
		||||
		// 计算中点索引 m
 | 
			
		||||
		m := i + (j-i)/2
 | 
			
		||||
		if nums[m] < target {
 | 
			
		||||
			// target 在区间 [m+1, j] 中
 | 
			
		||||
			i = m + 1
 | 
			
		||||
		} else if nums[m] > target {
 | 
			
		||||
			// target 在区间 [i, m-1] 中
 | 
			
		||||
			j = m - 1
 | 
			
		||||
		} else {
 | 
			
		||||
			// 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
			j = m - 1
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if i == len(nums) || nums[i] != target {
 | 
			
		||||
		// 未找到目标元素,返回 -1
 | 
			
		||||
		return -1
 | 
			
		||||
	}
 | 
			
		||||
	return i
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
func binarySearchRightEdge(nums []int, target int) int {
 | 
			
		||||
	// 初始化双闭区间 [0, n-1]
 | 
			
		||||
	i, j := 0, len(nums)-1
 | 
			
		||||
	for i <= j {
 | 
			
		||||
		// 计算中点索引 m
 | 
			
		||||
		m := i + (j-i)/2
 | 
			
		||||
		if nums[m] < target {
 | 
			
		||||
			// target 在区间 [m+1, j] 中
 | 
			
		||||
			i = m + 1
 | 
			
		||||
		} else if nums[m] > target {
 | 
			
		||||
			// target 在区间 [i, m-1] 中
 | 
			
		||||
			j = m - 1
 | 
			
		||||
		} else {
 | 
			
		||||
			// 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
			i = m + 1
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if j < 0 || nums[j] != target {
 | 
			
		||||
		// 未找到目标元素,返回 -1
 | 
			
		||||
		return -1
 | 
			
		||||
	}
 | 
			
		||||
	return j
 | 
			
		||||
}
 | 
			
		||||
@ -22,15 +22,3 @@ func TestBinarySearch(t *testing.T) {
 | 
			
		||||
		t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestBinarySearchEdge(t *testing.T) {
 | 
			
		||||
	target := 6
 | 
			
		||||
	nums := []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
 | 
			
		||||
	// 二分查找最左一个元素
 | 
			
		||||
	indexLeft := binarySearchLeftEdge(nums, target)
 | 
			
		||||
	fmt.Println("数组中最左一个元素 6 的索引 = ", indexLeft)
 | 
			
		||||
 | 
			
		||||
	// 二分查找最右一个元素
 | 
			
		||||
	indexRight := binarySearchRightEdge(nums, target)
 | 
			
		||||
	fmt.Println("数组中最右一个元素 6 的索引 = ", indexRight)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,56 +1,49 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.java
 | 
			
		||||
 * Created Time: 2023-05-21
 | 
			
		||||
 * Created Time: 2023-08-04
 | 
			
		||||
 * Author: Krahets (krahets@163.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package chapter_searching;
 | 
			
		||||
 | 
			
		||||
public class binary_search_edge {
 | 
			
		||||
    /* 二分查找最左一个元素 */
 | 
			
		||||
    /* 二分查找最左一个 target */
 | 
			
		||||
    static int binarySearchLeftEdge(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target)
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            else if (nums[m] > target)
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            else
 | 
			
		||||
                j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        // 等价于查找 target 的插入点
 | 
			
		||||
        int i = binary_search_insertion.binarySearchInsertion(nums, target);
 | 
			
		||||
        // 未找到 target ,返回 -1
 | 
			
		||||
        if (i == nums.length || nums[i] != target) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        if (i == nums.length || nums[i] != target)
 | 
			
		||||
            return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
        // 找到 target ,返回索引 i
 | 
			
		||||
        return i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* 二分查找最右一个元素 */
 | 
			
		||||
    /* 二分查找最右一个 target */
 | 
			
		||||
    static int binarySearchRightEdge(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target)
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            else if (nums[m] > target)
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            else
 | 
			
		||||
                i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        // 转化为查找最左一个 target + 1
 | 
			
		||||
        int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
 | 
			
		||||
        // j 指向最右一个 target ,i 指向首个大于 target 的元素
 | 
			
		||||
        int j = i - 1;
 | 
			
		||||
        // 未找到 target ,返回 -1
 | 
			
		||||
        if (j == -1 || nums[j] != target) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        if (j < 0 || nums[j] != target)
 | 
			
		||||
            return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
        // 找到 target ,返回索引 j
 | 
			
		||||
        return j;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        int target = 6;
 | 
			
		||||
        // 包含重复元素的数组
 | 
			
		||||
        int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
 | 
			
		||||
        System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
 | 
			
		||||
 | 
			
		||||
        // 二分查找最左一个元素
 | 
			
		||||
        int indexLeft = binarySearchLeftEdge(nums, target);
 | 
			
		||||
        System.out.println("数组中最左一个元素 6 的索引 = " + indexLeft);
 | 
			
		||||
 | 
			
		||||
        // 二分查找最右一个元素
 | 
			
		||||
        int indexRight = binarySearchRightEdge(nums, target);
 | 
			
		||||
        System.out.println("数组中最右一个元素 6 的索引 = " + indexRight);
 | 
			
		||||
        // 二分查找左边界和右边界
 | 
			
		||||
        for (int target : new int[] { 6, 7 }) {
 | 
			
		||||
            int index = binarySearchLeftEdge(nums, target);
 | 
			
		||||
            System.out.println("最左一个元素 " + target + " 的索引为 " + index);
 | 
			
		||||
            index = binarySearchRightEdge(nums, target);
 | 
			
		||||
            System.out.println("最右一个元素 " + target + " 的索引为 " + index);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										63
									
								
								codes/java/chapter_searching/binary_search_insertion.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						@ -0,0 +1,63 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.java
 | 
			
		||||
 * Created Time: 2023-08-04
 | 
			
		||||
 * Author: Krahets (krahets@163.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package chapter_searching;
 | 
			
		||||
 | 
			
		||||
class binary_search_insertion {
 | 
			
		||||
    /* 二分查找插入点(无重复元素) */
 | 
			
		||||
    static int binarySearchInsertionSimple(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target) {
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            } else if (nums[m] > target) {
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            } else {
 | 
			
		||||
                return m; // 找到 target ,返回插入点 m
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // 未找到 target ,返回插入点 i
 | 
			
		||||
        return i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* 二分查找插入点(存在重复元素) */
 | 
			
		||||
    static int binarySearchInsertion(int[] nums, int target) {
 | 
			
		||||
        int i = 0, j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
        while (i <= j) {
 | 
			
		||||
            int m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
            if (nums[m] < target) {
 | 
			
		||||
                i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
            } else if (nums[m] > target) {
 | 
			
		||||
                j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
            } else {
 | 
			
		||||
                j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // 返回插入点 i
 | 
			
		||||
        return i;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        // 无重复元素的数组
 | 
			
		||||
        int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
 | 
			
		||||
        System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
 | 
			
		||||
        // 二分查找插入点
 | 
			
		||||
        for (int target : new int[] { 6, 9 }) {
 | 
			
		||||
            int index = binarySearchInsertionSimple(nums, target);
 | 
			
		||||
            System.out.println("元素 " + target + " 的插入点的索引为 " + index);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 包含重复元素的数组
 | 
			
		||||
        nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
 | 
			
		||||
        System.out.println("\n数组 nums = " + java.util.Arrays.toString(nums));
 | 
			
		||||
        // 二分查找插入点
 | 
			
		||||
        for (int target : new int[] { 2, 6, 20 }) {
 | 
			
		||||
            int index = binarySearchInsertion(nums, target);
 | 
			
		||||
            System.out.println("元素 " + target + " 的插入点的索引为 " + index);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,57 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.js
 | 
			
		||||
 * Created Time: 2023-06-04
 | 
			
		||||
 * Author: Justin (xiefahit@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
function binarySearchLeftEdge(nums, target) {
 | 
			
		||||
    let i = 0,
 | 
			
		||||
        j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        let m = Math.floor((i + j) / 2); // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target) {
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (i === nums.length || nums[i] != target) {
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
function binarySearchRightEdge(nums, target) {
 | 
			
		||||
    let i = 0,
 | 
			
		||||
        j = nums.length - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        let m = Math.floor((i + j) / 2); // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target) {
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (j < 0 || nums[j] != target) {
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return j;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
let target = 6;
 | 
			
		||||
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
 | 
			
		||||
 | 
			
		||||
// 二分查找最左一个元素
 | 
			
		||||
let index_left = binarySearchLeftEdge(nums, target);
 | 
			
		||||
console.log('数组中最左一个元素 6 的索引 = ', index_left);
 | 
			
		||||
 | 
			
		||||
// 二分查找最右一个元素
 | 
			
		||||
let index_right = binarySearchRightEdge(nums, target);
 | 
			
		||||
console.log('数组中最右一个元素 6 的索引 = ', index_right);
 | 
			
		||||
@ -1,51 +1,48 @@
 | 
			
		||||
"""
 | 
			
		||||
File: binary_search_edge.py
 | 
			
		||||
Created Time: 2023-05-18
 | 
			
		||||
Created Time: 2023-08-04
 | 
			
		||||
Author: Krahets (krahets@163.com)
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
import sys, os.path as osp
 | 
			
		||||
 | 
			
		||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
 | 
			
		||||
from binary_search_insertion import binary_search_insertion
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def binary_search_left_edge(nums: list[int], target: int) -> int:
 | 
			
		||||
    """二分查找最左一个元素"""
 | 
			
		||||
    i, j = 0, len(nums) - 1  # 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while i <= j:
 | 
			
		||||
        m = (i + j) // 2  # 计算中点索引 m
 | 
			
		||||
        if nums[m] < target:
 | 
			
		||||
            i = m + 1  # target 在区间 [m+1, j] 中
 | 
			
		||||
        elif nums[m] > target:
 | 
			
		||||
            j = m - 1  # target 在区间 [i, m-1] 中
 | 
			
		||||
        else:
 | 
			
		||||
            j = m - 1  # 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
    """二分查找最左一个 target"""
 | 
			
		||||
    # 等价于查找 target 的插入点
 | 
			
		||||
    i = binary_search_insertion(nums, target)
 | 
			
		||||
    # 未找到 target ,返回 -1
 | 
			
		||||
    if i == len(nums) or nums[i] != target:
 | 
			
		||||
        return -1  # 未找到目标元素,返回 -1
 | 
			
		||||
        return -1
 | 
			
		||||
    # 找到 target ,返回索引 i
 | 
			
		||||
    return i
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def binary_search_right_edge(nums: list[int], target: int) -> int:
 | 
			
		||||
    """二分查找最右一个元素"""
 | 
			
		||||
    i, j = 0, len(nums) - 1  # 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while i <= j:
 | 
			
		||||
        m = (i + j) // 2  # 计算中点索引 m
 | 
			
		||||
        if nums[m] < target:
 | 
			
		||||
            i = m + 1  # target 在区间 [m+1, j] 中
 | 
			
		||||
        elif nums[m] > target:
 | 
			
		||||
            j = m - 1  # target 在区间 [i, m-1] 中
 | 
			
		||||
        else:
 | 
			
		||||
            i = m + 1  # 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
    if j < 0 or nums[j] != target:
 | 
			
		||||
        return -1  # 未找到目标元素,返回 -1
 | 
			
		||||
    """二分查找最右一个 target"""
 | 
			
		||||
    # 转化为查找最左一个 target + 1
 | 
			
		||||
    i = binary_search_insertion(nums, target + 1)
 | 
			
		||||
    # j 指向最右一个 target ,i 指向首个大于 target 的元素
 | 
			
		||||
    j = i - 1
 | 
			
		||||
    # 未找到 target ,返回 -1
 | 
			
		||||
    if j == -1 or nums[j] != target:
 | 
			
		||||
        return -1
 | 
			
		||||
    # 找到 target ,返回索引 j
 | 
			
		||||
    return j
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
"""Driver Code"""
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    target = 6
 | 
			
		||||
    # 包含重复元素的数组
 | 
			
		||||
    nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
 | 
			
		||||
    print(f"\n数组 nums = {nums}")
 | 
			
		||||
 | 
			
		||||
    # 二分查找最左一个元素
 | 
			
		||||
    index_left = binary_search_left_edge(nums, target)
 | 
			
		||||
    print("数组中最左一个元素 6 的索引 = ", index_left)
 | 
			
		||||
 | 
			
		||||
    # 二分查找最右一个元素
 | 
			
		||||
    index_right = binary_search_right_edge(nums, target)
 | 
			
		||||
    print("数组中最右一个元素 6 的索引 = ", index_right)
 | 
			
		||||
    # 二分查找左边界和右边界
 | 
			
		||||
    for target in [6, 7]:
 | 
			
		||||
        index = binary_search_left_edge(nums, target)
 | 
			
		||||
        print(f"最左一个元素 {target} 的索引为 {index}")
 | 
			
		||||
        index = binary_search_right_edge(nums, target)
 | 
			
		||||
        print(f"最右一个元素 {target} 的索引为 {index}")
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										54
									
								
								codes/python/chapter_searching/binary_search_insertion.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						@ -0,0 +1,54 @@
 | 
			
		||||
"""
 | 
			
		||||
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) // 2  # 计算中点索引 m
 | 
			
		||||
        if nums[m] < target:
 | 
			
		||||
            i = m + 1  # target 在区间 [m+1, j] 中
 | 
			
		||||
        elif nums[m] > target:
 | 
			
		||||
            j = m - 1  # target 在区间 [i, m-1] 中
 | 
			
		||||
        else:
 | 
			
		||||
            return m  # 找到 target ,返回插入点 m
 | 
			
		||||
    # 未找到 target ,返回插入点 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) // 2  # 计算中点索引 m
 | 
			
		||||
        if nums[m] < target:
 | 
			
		||||
            i = m + 1  # target 在区间 [m+1, j] 中
 | 
			
		||||
        elif nums[m] > target:
 | 
			
		||||
            j = m - 1  # target 在区间 [i, m-1] 中
 | 
			
		||||
        else:
 | 
			
		||||
            j = m - 1  # 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
    # 返回插入点 i
 | 
			
		||||
    return i
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
"""Driver Code"""
 | 
			
		||||
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}")
 | 
			
		||||
@ -1,59 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * File: binary_search_edge.rs
 | 
			
		||||
 * Created Time: 2023-05-31
 | 
			
		||||
 * Author: WSL0809 (wslzzy@outlook.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
fn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {
 | 
			
		||||
    let mut i = 0;
 | 
			
		||||
    let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while i <= j {
 | 
			
		||||
        let m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
        if nums[m as usize] < target {
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if nums[m as usize] > target {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if i == nums.len() as i32 || nums[i as usize] != target {
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    i
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
fn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {
 | 
			
		||||
    let mut i = 0;
 | 
			
		||||
    let mut j = nums.len() as i32 - 1; // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while i <= j {
 | 
			
		||||
        let m = i + (j - i) / 2; // 计算中点索引 m
 | 
			
		||||
        if nums[m as usize] < target {
 | 
			
		||||
            i = m + 1; // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if nums[m as usize] > target {
 | 
			
		||||
            j = m - 1; // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if j < 0 || nums[j as usize] != target {
 | 
			
		||||
        return -1; // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    j
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    let target = 6;
 | 
			
		||||
    let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
 | 
			
		||||
 | 
			
		||||
    // 二分查找最左一个元素
 | 
			
		||||
    let index_left = binary_search_left_edge(&nums, target);
 | 
			
		||||
    println!("数组中最左一个元素 6 的索引 = {}", index_left);
 | 
			
		||||
 | 
			
		||||
    // 二分查找最右一个元素
 | 
			
		||||
    let index_right = binary_search_right_edge(&nums, target);
 | 
			
		||||
    println!("数组中最右一个元素 6 的索引 = {}", index_right);
 | 
			
		||||
}
 | 
			
		||||
@ -1,64 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.swift
 | 
			
		||||
 * Created Time: 2023-05-28
 | 
			
		||||
 * Author: nuomi1 (nuomi1@qq.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
func binarySearchLeftEdge(nums: [Int], target: Int) -> Int {
 | 
			
		||||
    // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    var i = 0
 | 
			
		||||
    var j = nums.count - 1
 | 
			
		||||
    while i <= j {
 | 
			
		||||
        let m = i + (j - 1) / 2 // 计算中点索引 m
 | 
			
		||||
        if nums[m] < target {
 | 
			
		||||
            i = m + 1 // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if nums[m] > target {
 | 
			
		||||
            j = m - 1 // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1 // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if i == nums.count || nums[i] != target {
 | 
			
		||||
        return -1 // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return i
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
func binarySearchRightEdge(nums: [Int], target: Int) -> Int {
 | 
			
		||||
    // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    var i = 0
 | 
			
		||||
    var j = nums.count - 1
 | 
			
		||||
    while i <= j {
 | 
			
		||||
        let m = i + (j - i) / 2 // 计算中点索引 m
 | 
			
		||||
        if nums[m] < target {
 | 
			
		||||
            i = m + 1 // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if nums[m] > target {
 | 
			
		||||
            j = m - 1 // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            i = m + 1 // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if j < 0 || nums[j] != target {
 | 
			
		||||
        return -1 // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return j
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@main
 | 
			
		||||
enum BinarySearchEdge {
 | 
			
		||||
    /* Driver Code */
 | 
			
		||||
    static func main() {
 | 
			
		||||
        let target = 6
 | 
			
		||||
        let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15]
 | 
			
		||||
 | 
			
		||||
        // 二分查找最左一个元素
 | 
			
		||||
        let indexLeft = binarySearchLeftEdge(nums: nums, target: target)
 | 
			
		||||
        print("数组中最左一个元素 6 的索引 = \(indexLeft)")
 | 
			
		||||
 | 
			
		||||
        // 二分查找最右一个元素
 | 
			
		||||
        let indexRight = binarySearchRightEdge(nums: nums, target: target)
 | 
			
		||||
        print("数组中最右一个元素 6 的索引 = \(indexRight)")
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,55 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * File: binary_search_edge.ts
 | 
			
		||||
 * Created Time: 2023-06-04
 | 
			
		||||
 * Author: Justin (xiefahit@gmail.com)
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 二分查找最左一个元素 */
 | 
			
		||||
function binarySearchLeftEdge(nums: number[], target: number): number {
 | 
			
		||||
    let i = 0, j = nums.length - 1;  // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        let m = Math.floor((i + j) / 2);  // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target) {
 | 
			
		||||
            i = m + 1;  // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1;  // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            j = m - 1;  // 首个小于 target 的元素在区间 [i, m-1] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (i === nums.length || nums[i] != target) {
 | 
			
		||||
        return -1;  // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 二分查找最右一个元素 */
 | 
			
		||||
function binarySearchRightEdge(nums: number[], target: number): number {
 | 
			
		||||
    let i = 0, j = nums.length - 1;  // 初始化双闭区间 [0, n-1]
 | 
			
		||||
    while (i <= j) {
 | 
			
		||||
        let m = Math.floor((i + j) / 2);  // 计算中点索引 m
 | 
			
		||||
        if (nums[m] < target) {
 | 
			
		||||
            i = m + 1;  // target 在区间 [m+1, j] 中
 | 
			
		||||
        } else if (nums[m] > target) {
 | 
			
		||||
            j = m - 1;  // target 在区间 [i, m-1] 中
 | 
			
		||||
        } else {
 | 
			
		||||
            i = m + 1;  // 首个大于 target 的元素在区间 [m+1, j] 中
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (j < 0 || nums[j] != target) {
 | 
			
		||||
        return -1;  // 未找到目标元素,返回 -1
 | 
			
		||||
    }
 | 
			
		||||
    return j;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
let target: number = 6;
 | 
			
		||||
const nums: number[] = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
 | 
			
		||||
 | 
			
		||||
// 二分查找最左一个元素
 | 
			
		||||
let index_left: number = binarySearchLeftEdge(nums, target);
 | 
			
		||||
console.log("数组中最左一个元素 6 的索引 = ", index_left);
 | 
			
		||||
 | 
			
		||||
// 二分查找最右一个元素
 | 
			
		||||
let index_right: number = binarySearchRightEdge(nums, target);
 | 
			
		||||
console.log("数组中最右一个元素 6 的索引 = ", index_right);
 | 
			
		||||
| 
		 After Width: | Height: | Size: 58 KiB  | 
| 
		 Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 75 KiB  | 
| 
		 Before Width: | Height: | Size: 62 KiB  | 
| 
		 Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 54 KiB  | 
| 
		 Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 50 KiB  | 
| 
		 Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 58 KiB  | 
| 
		 Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 47 KiB  | 
| 
		 Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 57 KiB  | 
| 
		 Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 47 KiB  | 
| 
		 Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 51 KiB  | 
@ -6,7 +6,9 @@
 | 
			
		||||
 | 
			
		||||
    给定一个长度为 $n$ 的数组 `nums` ,元素按从小到大的顺序排列,数组不包含重复元素。请查找并返回元素 `target` 在该数组中的索引。若数组不包含该元素,则返回 $-1$ 。
 | 
			
		||||
 | 
			
		||||
对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。其中,中括号表示“闭区间”,即包含边界值本身。
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
对于上述问题,我们先初始化指针 $i = 0$ 和 $j = n - 1$ ,分别指向数组首元素和尾元素,代表搜索区间 $[0, n - 1]$ 。请注意,中括号表示闭区间,其包含边界值本身。
 | 
			
		||||
 | 
			
		||||
接下来,循环执行以下两个步骤:
 | 
			
		||||
 | 
			
		||||
@ -18,9 +20,6 @@
 | 
			
		||||
 | 
			
		||||
若数组不包含目标元素,搜索区间最终会缩小为空。此时返回 $-1$ 。
 | 
			
		||||
 | 
			
		||||
=== "<0>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<1>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
		 After Width: | Height: | Size: 52 KiB  | 
| 
		 Before Width: | Height: | Size: 46 KiB  | 
| 
		 Before Width: | Height: | Size: 59 KiB  | 
| 
		 Before Width: | Height: | Size: 55 KiB  | 
| 
		 Before Width: | Height: | Size: 64 KiB  | 
| 
		 Before Width: | Height: | Size: 55 KiB  | 
| 
		 Before Width: | Height: | Size: 63 KiB  | 
| 
		 Before Width: | Height: | Size: 55 KiB  | 
| 
		 Before Width: | Height: | Size: 60 KiB  | 
| 
		 Before Width: | Height: | Size: 53 KiB  | 
| 
		 Before Width: | Height: | Size: 78 KiB  | 
| 
		 After Width: | Height: | Size: 50 KiB  | 
@ -1,56 +1,19 @@
 | 
			
		||||
# 二分查找边界
 | 
			
		||||
 | 
			
		||||
在上一节中,题目规定数组中所有元素都是唯一的。如果目标元素在数组中多次出现,上节介绍的方法只能保证返回其中一个目标元素的索引,**而无法确定该索引的左边和右边还有多少目标元素**。
 | 
			
		||||
## 查找左边界
 | 
			
		||||
 | 
			
		||||
!!! question
 | 
			
		||||
 | 
			
		||||
    给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请查找并返回元素 `target` 在数组中首次出现的索引。若数组中不包含该元素,则返回 $-1$ 。
 | 
			
		||||
    给定一个长度为 $n$ 的有序数组 `nums` ,数组可能包含重复元素。请返回数组中最左一个元素 `target` 的索引。若数组中不包含该元素,则返回 $-1$ 。
 | 
			
		||||
 | 
			
		||||
## 线性方法
 | 
			
		||||
回忆二分查找插入点的方法,搜索完成后,$i$ 指向最左一个 `target` ,**因此查找插入点本质上是在查找最左一个 `target` 的索引**。
 | 
			
		||||
 | 
			
		||||
为了查找数组中最左边的 `target` ,我们可以分为两步:
 | 
			
		||||
考虑通过查找插入点的函数实现查找左边界。请注意,数组中可能不包含 `target` ,此时有两种可能:
 | 
			
		||||
 | 
			
		||||
1. 进行二分查找,定位到任意一个 `target` 的索引,记为 $k$ 。
 | 
			
		||||
2. 以索引 $k$ 为起始点,向左进行线性遍历,找到最左边的 `target` 返回即可。
 | 
			
		||||
1. 插入点的索引 $i$ 越界;
 | 
			
		||||
2. 元素 `nums[i]` 与 `target` 不相等;
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
这个方法虽然有效,但由于包含线性查找,时间复杂度为 $O(n)$ ,当存在很多重复的 `target` 时效率较低。
 | 
			
		||||
 | 
			
		||||
## 二分方法
 | 
			
		||||
 | 
			
		||||
考虑仅使用二分查找解决该问题。整体算法流程不变,先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系:
 | 
			
		||||
 | 
			
		||||
- 当 `nums[m] < target` 或 `nums[m] > target` 时,说明还没有找到 `target` ,因此采取与上节代码相同的缩小区间操作,**从而使指针 $i$ 和 $j$ 向 `target` 靠近**。
 | 
			
		||||
- 当 `nums[m] == target` 时,说明“小于 `target` 的元素”在区间 $[i, m - 1]$ 中,因此采用 $j = m - 1$ 来缩小区间,**从而使指针 $j$ 向小于 `target` 的元素靠近**。
 | 
			
		||||
 | 
			
		||||
二分查找完成后,**$i$ 指向最左边的 `target` ,$j$ 指向首个小于 `target` 的元素**,因此返回索引 $i$ 即可。
 | 
			
		||||
 | 
			
		||||
=== "<1>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<2>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<3>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<4>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<5>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<6>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<7>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<8>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
注意,数组可能不包含目标元素 `target` 。因此在函数返回前,我们需要先判断 `nums[i]` 与 `target` 是否相等,以及索引 $i$ 是否越界。
 | 
			
		||||
当遇到以上两种情况时,直接返回 $-1$ 即可。
 | 
			
		||||
 | 
			
		||||
=== "Java"
 | 
			
		||||
 | 
			
		||||
@ -126,9 +89,19 @@
 | 
			
		||||
 | 
			
		||||
## 查找右边界
 | 
			
		||||
 | 
			
		||||
类似地,我们也可以二分查找最右边的 `target` 。当 `nums[m] == target` 时,说明大于 `target` 的元素在区间 $[m + 1, j]$ 中,因此执行 `i = m + 1` ,**使得指针 $i$ 向大于 `target` 的元素靠近**。
 | 
			
		||||
那么如何查找最右一个 `target` 呢?最直接的方式是修改代码,替换在 `nums[m] == target` 情况下的指针收缩操作。代码在此省略,有兴趣的同学可以自行实现。
 | 
			
		||||
 | 
			
		||||
完成二分后,**$i$ 指向首个大于 `target` 的元素,$j$ 指向最右边的 `target`** ,因此返回索引 $j$ 即可。
 | 
			
		||||
下面我们介绍两种更加取巧的方法。
 | 
			
		||||
 | 
			
		||||
### 复用查找左边界
 | 
			
		||||
 | 
			
		||||
实际上,我们可以利用查找最左元素的函数来查找最右元素,具体方法为:**将查找最右一个 `target` 转化为查找最左一个 `target + 1`**。
 | 
			
		||||
 | 
			
		||||
查找完成后,指针 $i$ 指向最左一个 `target + 1`(如果存在),而 $j$ 指向最右一个 `target` ,**因此返回 $j$ 即可**。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
请注意,返回的插入点是 $i$ ,因此需要将其减 $1$ ,从而获得 $j$ 。
 | 
			
		||||
 | 
			
		||||
=== "Java"
 | 
			
		||||
 | 
			
		||||
@ -202,10 +175,18 @@
 | 
			
		||||
    [class]{}-[func]{binary_search_right_edge}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
观察下图,搜索最右边元素时指针 $j$ 的作用与搜索最左边元素时指针 $i$ 的作用一致,反之亦然。也就是说,**搜索最左边元素和最右边元素的实现是镜像对称的**。
 | 
			
		||||
### 转化为查找元素
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
我们知道,当数组不包含 `target` 时,最后 $i$ , $j$ 会分别指向首个大于、小于 `target` 的元素。
 | 
			
		||||
 | 
			
		||||
!!! tip
 | 
			
		||||
根据上述结论,我们可以构造一个数组中不存在的元素,用于查找左右边界:
 | 
			
		||||
 | 
			
		||||
    以上代码采取的都是“双闭区间”写法。有兴趣的读者可以自行实现“左闭右开”写法。
 | 
			
		||||
- 查找最左一个 `target` :可以转化为查找 `target - 0.5` ,并返回指针 $i$ 。
 | 
			
		||||
- 查找最右一个 `target` :可以转化为查找 `target + 0.5` ,并返回指针 $j$ 。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
代码在此省略,值得注意的有:
 | 
			
		||||
 | 
			
		||||
- 给定数组不包含小数,这意味着我们无需关心如何处理相等的情况。
 | 
			
		||||
- 因为该方法引入了小数,所以需要将函数中的变量 `target` 改为浮点数类型。
 | 
			
		||||
 | 
			
		||||
| 
		 After Width: | Height: | Size: 58 KiB  | 
| 
		 After Width: | Height: | Size: 46 KiB  | 
| 
		 After Width: | Height: | Size: 52 KiB  | 
| 
		 After Width: | Height: | Size: 47 KiB  | 
| 
		 After Width: | Height: | Size: 56 KiB  | 
| 
		 After Width: | Height: | Size: 46 KiB  | 
| 
		 After Width: | Height: | Size: 56 KiB  | 
| 
		 After Width: | Height: | Size: 47 KiB  | 
| 
		 After Width: | Height: | Size: 55 KiB  | 
| 
		 After Width: | Height: | Size: 45 KiB  | 
							
								
								
									
										227
									
								
								docs/chapter_searching/binary_search_insertion.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						@ -0,0 +1,227 @@
 | 
			
		||||
# 二分查找插入点
 | 
			
		||||
 | 
			
		||||
二分查找不仅可用于搜索目标元素,还具有许多变种问题,比如搜索目标元素的插入位置。
 | 
			
		||||
 | 
			
		||||
## 无重复元素的情况
 | 
			
		||||
 | 
			
		||||
!!! question
 | 
			
		||||
 | 
			
		||||
    给定一个长度为 $n$ 的有序数组 `nums` 和一个元素 `target` ,数组不存在重复元素。现将 `target` 插入到数组 `nums` 中,并保持其有序性。若数组中已存在元素 `target` ,则插入到其左方。请返回插入后 `target` 在数组中的索引。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
如果想要复用上节的二分查找代码,则需要回答以下两个问题。
 | 
			
		||||
 | 
			
		||||
**问题一**:当数组中包含 `target` 时,插入点的索引是否是该元素的索引?
 | 
			
		||||
 | 
			
		||||
题目要求将 `target` 插入到相等元素的左边,这意味着新插入的 `target` 替换了原来 `target` 的位置。也就是说,**当数组包含 `target` 时,插入点的索引就是该 `target` 的索引**。
 | 
			
		||||
 | 
			
		||||
**问题二**:当数组中不存在 `target` 时,插入点是哪个元素的索引?
 | 
			
		||||
 | 
			
		||||
进一步思考二分查找过程:当 `nums[m] < target` 时 $i$ 移动,这意味着指针 $i$ 在向大于等于 `target` 的元素靠近。同理,指针 $j$ 始终在向小于等于 `target` 的元素靠近。
 | 
			
		||||
 | 
			
		||||
因此二分结束时一定有:$i$ 指向首个大于 `target` 的元素,$j$ 指向首个小于 `target` 的元素。**易得当数组不包含 `target` 时,插入索引为 $i$** 。
 | 
			
		||||
 | 
			
		||||
=== "Java"
 | 
			
		||||
 | 
			
		||||
    ```java title="binary_search_insertion.java"
 | 
			
		||||
    [class]{binary_search_insertion}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C++"
 | 
			
		||||
 | 
			
		||||
    ```cpp title="binary_search_insertion.cpp"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Python"
 | 
			
		||||
 | 
			
		||||
    ```python title="binary_search_insertion.py"
 | 
			
		||||
    [class]{}-[func]{binary_search_insertion_simple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Go"
 | 
			
		||||
 | 
			
		||||
    ```go title="binary_search_insertion.go"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "JS"
 | 
			
		||||
 | 
			
		||||
    ```javascript title="binary_search_insertion.js"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "TS"
 | 
			
		||||
 | 
			
		||||
    ```typescript title="binary_search_insertion.ts"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C"
 | 
			
		||||
 | 
			
		||||
    ```c title="binary_search_insertion.c"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C#"
 | 
			
		||||
 | 
			
		||||
    ```csharp title="binary_search_insertion.cs"
 | 
			
		||||
    [class]{binary_search_insertion}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Swift"
 | 
			
		||||
 | 
			
		||||
    ```swift title="binary_search_insertion.swift"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Zig"
 | 
			
		||||
 | 
			
		||||
    ```zig title="binary_search_insertion.zig"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Dart"
 | 
			
		||||
 | 
			
		||||
    ```dart title="binary_search_insertion.dart"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertionSimple}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Rust"
 | 
			
		||||
 | 
			
		||||
    ```rust title="binary_search_insertion.rs"
 | 
			
		||||
    [class]{}-[func]{binary_search_insertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
## 存在重复元素的情况
 | 
			
		||||
 | 
			
		||||
!!! question
 | 
			
		||||
 | 
			
		||||
    在上一题的基础上,规定数组可能包含重复元素,其余不变。
 | 
			
		||||
 | 
			
		||||
假设数组中存在多个 `target` ,则普通二分查找只能返回其中一个 `target` 的索引,**而无法确定该元素的左边和右边还有多少 `target`**。
 | 
			
		||||
 | 
			
		||||
题目要求将目标元素插入到最左边,**所以我们需要查找数组中最左一个 `target` 的索引**。初步考虑通过以下两步实现:
 | 
			
		||||
 | 
			
		||||
1. 执行二分查找,得到任意一个 `target` 的索引,记为 $k$ 。
 | 
			
		||||
2. 从索引 $k$ 开始,向左进行线性遍历,当找到最左边的 `target` 时返回。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
此方法虽然可用,但其包含线性查找,因此时间复杂度为 $O(n)$ 。当数组中存在很多重复的 `target` 时,该方法效率很低。
 | 
			
		||||
 | 
			
		||||
现考虑修改二分查找代码。整体流程不变,每轮先计算中点索引 $m$ ,再判断 `target` 和 `nums[m]` 大小关系:
 | 
			
		||||
 | 
			
		||||
1. 当 `nums[m] < target` 或 `nums[m] > target` 时,说明还没有找到 `target` ,因此采用普通二分查找的缩小区间操作,**从而使指针 $i$ 和 $j$ 向 `target` 靠近**。
 | 
			
		||||
2. 当 `nums[m] == target` 时,说明小于 `target` 的元素在区间 $[i, m - 1]$ 中,因此采用 $j = m - 1$ 来缩小区间,**从而使指针 $j$ 向小于 `target` 的元素靠近**。
 | 
			
		||||
 | 
			
		||||
循环完成后,$i$ 指向最左边的 `target` ,$j$ 指向首个小于 `target` 的元素,**因此索引 $i$ 就是插入点**。
 | 
			
		||||
 | 
			
		||||
=== "<1>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<2>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<3>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<4>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<5>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<6>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<7>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
=== "<8>"
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
观察以下代码,判断分支 `nums[m] > target` 和 `nums[m] == target` 的操作相同,因此两者可以合并。
 | 
			
		||||
 | 
			
		||||
即便如此,我们仍然可以将判断条件保持展开,因为其逻辑更加清晰、可读性更好。
 | 
			
		||||
 | 
			
		||||
=== "Java"
 | 
			
		||||
 | 
			
		||||
    ```java title="binary_search_insertion.java"
 | 
			
		||||
    [class]{binary_search_insertion}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C++"
 | 
			
		||||
 | 
			
		||||
    ```cpp title="binary_search_insertion.cpp"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Python"
 | 
			
		||||
 | 
			
		||||
    ```python title="binary_search_insertion.py"
 | 
			
		||||
    [class]{}-[func]{binary_search_insertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Go"
 | 
			
		||||
 | 
			
		||||
    ```go title="binary_search_insertion.go"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "JS"
 | 
			
		||||
 | 
			
		||||
    ```javascript title="binary_search_insertion.js"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "TS"
 | 
			
		||||
 | 
			
		||||
    ```typescript title="binary_search_insertion.ts"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C"
 | 
			
		||||
 | 
			
		||||
    ```c title="binary_search_insertion.c"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "C#"
 | 
			
		||||
 | 
			
		||||
    ```csharp title="binary_search_insertion.cs"
 | 
			
		||||
    [class]{binary_search_insertion}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Swift"
 | 
			
		||||
 | 
			
		||||
    ```swift title="binary_search_insertion.swift"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Zig"
 | 
			
		||||
 | 
			
		||||
    ```zig title="binary_search_insertion.zig"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Dart"
 | 
			
		||||
 | 
			
		||||
    ```dart title="binary_search_insertion.dart"
 | 
			
		||||
    [class]{}-[func]{binarySearchInsertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
=== "Rust"
 | 
			
		||||
 | 
			
		||||
    ```rust title="binary_search_insertion.rs"
 | 
			
		||||
    [class]{}-[func]{binary_search_insertion}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
!!! tip
 | 
			
		||||
 | 
			
		||||
    本节的代码都是“双闭区间”写法。有兴趣的读者可以自行实现“左闭右开”写法。
 | 
			
		||||
 | 
			
		||||
总的来看,二分查找无非就是给指针 $i$ , $j$ 分别设定搜索目标,目标可能是一个具体的元素(例如 `target` ),也可能是一个元素范围(例如小于 `target` 的元素)。
 | 
			
		||||
 | 
			
		||||
在不断的循环二分中,指针 $i$ , $j$ 都逐渐逼近预先设定的目标。最终,它们或是成功找到答案,或是越过边界后停止。
 | 
			
		||||
							
								
								
									
										11
									
								
								mkdocs.yml
									
									
									
									
									
								
							
							
						
						@ -207,10 +207,13 @@ nav:
 | 
			
		||||
    # [icon: material/text-search]
 | 
			
		||||
    - chapter_searching/index.md
 | 
			
		||||
    - 10.1.   二分查找: chapter_searching/binary_search.md
 | 
			
		||||
    - 10.2.   二分查找边界: chapter_searching/binary_search_edge.md 
 | 
			
		||||
    - 10.3.   哈希优化策略: chapter_searching/replace_linear_by_hashing.md
 | 
			
		||||
    - 10.4.   重识搜索算法: chapter_searching/searching_algorithm_revisited.md
 | 
			
		||||
    - 10.5.   小结: chapter_searching/summary.md
 | 
			
		||||
    # [status: new]
 | 
			
		||||
    - 10.2.   二分查找插入点: chapter_searching/binary_search_insertion.md
 | 
			
		||||
    # [status: new]
 | 
			
		||||
    - 10.3.   二分查找边界: chapter_searching/binary_search_edge.md
 | 
			
		||||
    - 10.4.   哈希优化策略: chapter_searching/replace_linear_by_hashing.md
 | 
			
		||||
    - 10.5.   重识搜索算法: chapter_searching/searching_algorithm_revisited.md
 | 
			
		||||
    - 10.6.   小结: chapter_searching/summary.md
 | 
			
		||||
  - 11.   排序:
 | 
			
		||||
    # [icon: material/sort-ascending]
 | 
			
		||||
    - chapter_sorting/index.md
 | 
			
		||||
 | 
			
		||||