forked from tekintian/java_se_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSplitFindDemo.java
113 lines (90 loc) · 3.23 KB
/
TwoSplitFindDemo.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cn.tekin.base;
import cn.tekin.utils.sort.QuickSort;
/**
* 二分法查找 演示
*/
public class TwoSplitFindDemo {
public static void main(String[] args) {
// int arr[]={88,4,2,5,7,0,6,88,99,25,95,-6,-99,-23,560,1234};
// BinaryFind bf=new BinaryFind();
// bf.find(0,arr.length-1,-6, arr);
//二分查找的前提,数据必须是从小到大排列的有序数据
int []arr1={88,99,555,8,1,-99,-8,-5,-1,1,2,3,4,99,12,822,25,1035,36,48,99,999};
//使用快速排序法对数组进行排序
arr1 = QuickSort.sort(arr1,0,arr1.length-1);
//定义一个数组接收排序后的数组
System.out.print("\n\r 排序后的数组:{");
for (int i = 0; i < arr1.length; i++) {
if (i>=0&&i<arr1.length-1){
System.out.print(arr1[i] +", ");
}else{
System.out.print(arr1[i] +"}");
}
}
System.out.println("\r\n");
TwoSpiltFind tsf=new TwoSpiltFind();
//默认左边的索引为 0, 一位数组的索引是从0开始的, 左边的index是数组个数(长度)-1
int rightIndex =arr1.length-1;
tsf.find(arr1, 1,0,arr1.length-1);
// System.out.println("普通方式实现:"+TwoSpiltFind.binSearch(arr1, -1));
}
}
/**
* 二分查找法 实现类
*/
class TwoSpiltFind{
public void find(int []arr, int val, int leftIndex, int rightIndex){
//首先找到中间的index
int midIndex = (rightIndex-leftIndex)/2 +leftIndex;
//获取中间值
int midVal=arr[midIndex];
//右边的index 大于左边的index 说明数据正确,开始查找
if (rightIndex>=leftIndex){
//如果中间值 大于要查找的值,则在左边找
if (midVal>val){
find(arr, val, leftIndex, midIndex-1);
}else if (midVal<val){
//在arr的右边找数
find(arr, val, midIndex+1,rightIndex);
}else if (midVal==val){
System.out.println("找到了, 下标为:"+midIndex);
}
}
}
// 二分查找递归实现
public static int binSearch(int srcArray[], int start, int end, int val) {
//中间索引
int mid = (end - start) / 2 + start;
if (srcArray[mid] == val) {
return mid;
}
if (start >= end) {
return -1;
} else if (val > srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, val);
} else if (val < srcArray[mid]) {
return binSearch(srcArray, start, mid - 1, val);
}
return -1;
}
// 二分查找普通循环实现
public static int binSearch(int srcArray[], int val) {
int mid = srcArray.length / 2;
if (val == srcArray[mid]) {
return mid;
}
int start = 0;
int end = srcArray.length - 1;
while (start <= end) {
mid = (end - start) / 2 + start;
if (val < srcArray[mid]) {
end = mid - 1;
} else if (val > srcArray[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
}