-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHalfSearch.java
More file actions
37 lines (34 loc) · 1.09 KB
/
HalfSearch.java
File metadata and controls
37 lines (34 loc) · 1.09 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
package SortPractice;
import java.util.Random;
/**
* @auther: wilson
* @version: v1.0 创建时间: 2019-12-23:2019
* Des:折半查找
*/
public class HalfSearch {
public static void main(String[] args) {
Integer[] dataSouce = CommonAndMain.RamdomArray();
dataSouce = BubbleSort.bubbleSort(dataSouce);
Random random = new Random();
Integer integer = random.nextInt(50);
System.out.println(integer);
System.out.println(halfFind(dataSouce,integer));
}
public static Boolean halfFind(Integer[] sortrdArray, Integer target){
Integer high = sortrdArray.length-1;
Integer low = 0;
Integer middle = (high - low)/2 + low;
while (middle > low && middle < high){
if (target.equals(sortrdArray[middle])){
return true;
} else if (target < sortrdArray[middle]) {
high = middle;
middle = (high - low) / 2 +low;
}else {
low = middle;
middle = (high - low) / 2 +low;
}
}
return false;
}
}