-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBinarySearch.java
More file actions
100 lines (90 loc) · 3.37 KB
/
BinarySearch.java
File metadata and controls
100 lines (90 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package search.binary;
/**
* 二分搜索(Binary Search)- Java 版本
*
* 要求数组有序(升序),通过不断折半缩小搜索区间。
* 时间复杂度:O(log n)
* 空间复杂度:O(1) 迭代;O(log n) 递归
*/
public class BinarySearch {
// 迭代版二分搜索
public static int binarySearchIterative(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 递归版二分搜索
public static int binarySearchRecursive(int[] arr, int target, int left, int right) {
if (left > right) return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) return binarySearchRecursive(arr, target, mid + 1, right);
return binarySearchRecursive(arr, target, left, mid - 1);
}
// 查找第一个出现的位置(左边界)
public static int findFirstOccurrence(int[] arr, int target) {
int left = 0, right = arr.length - 1, result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// 查找最后一个出现的位置(右边界)
public static int findLastOccurrence(int[] arr, int target) {
int left = 0, right = arr.length - 1, result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid;
left = mid + 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// 查找插入位置(lower bound)
public static int findInsertPosition(int[] arr, int target) {
int left = 0, right = arr.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public static void main(String[] args) {
System.out.println("========== Binary Search (Java) ==========");
int[] arr = {1, 3, 5, 7, 9, 11, 13};
System.out.println("数组: " + java.util.Arrays.toString(arr));
System.out.println("迭代查找 7 -> " + binarySearchIterative(arr, 7));
System.out.println("递归查找 7 -> " + binarySearchRecursive(arr, 7, 0, arr.length - 1));
int[] arr2 = {1, 3, 5, 5, 5, 7, 9};
System.out.println("\n数组2: " + java.util.Arrays.toString(arr2));
System.out.println("第一个 5 的位置 -> " + findFirstOccurrence(arr2, 5));
System.out.println("最后一个 5 的位置 -> " + findLastOccurrence(arr2, 5));
System.out.println("插入 6 的位置 -> " + findInsertPosition(arr2, 6));
}
}