-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seach.java
61 lines (51 loc) · 1.09 KB
/
Seach.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
package seach;
import com.mo.sort.Sort;
/**
* @author 文龙
* @version 2017-11-27 下午9:20:11
*
* 查找
*
*/
public class Seach {
//在无序中查找
public static int seqSeach(int[] a, int obj) {
int index = 0;
for (int i : a) {
if(i == obj) {
return index;
}
index++;
}
return -1;
}
//顺序查找
public static int orderSeqSearch(int[] a,int obj) {
int i = 0;
int n = a.length;
while(i < a.length - 1 && obj > a[i]) i++;
if(obj == a[i]) return i;
else return -1;
}
//二分查找
public static int bitSeach(int[] a,int obj) {
int low = 0;
int high = a.length - 1;
int mid;
while(low <= high) {
mid = (low + high)/2;
if(a[mid] == obj) return mid;
else if (a[mid] < obj) low = mid + 1;
else high = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int[] a = {2,1,999,6,2,8,3,6,7,8,9,7,8,7,6,5,5,4,2,34,5,66,777,6,5,5,41};
a = Sort.dubbleSortM(a);
/*System.out.println(seqSeach(a, 41));
System.out.println(a.length - 1);*/
System.out.println(orderSeqSearch(a, 100000000));
System.out.println(bitSeach(a, 777));
}
}