- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.java
More file actions
Latest commit
29 lines (28 loc) · 881 Bytes
/
Copy pathbinarySearch.java
File metadata and controls
29 lines (28 loc) · 881 Bytes
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
importorg.json.JSONArray;
publicclassbinarySearch {
publicintsearch(JSONArraydataArray,StringsearchElement){
try{
intlowerBound = 0;
intupperBound = dataArray.length() - 1;
while (lowerBound <= upperBound){
intmid = lowerBound + (upperBound - lowerBound) / 2;
System.out.println(mid);
intcompareValue = searchElement.compareTo(dataArray.getString(mid));
if(compareValue == 0){
returnmid;
}
if(compareValue > 0){
lowerBound = mid + 1;
}
else{
upperBound = mid - 1;
}
}
return -1;
}
catch (Exceptione){
e.printStackTrace();
return -1;
}
}
}