public class Treearithmetic
{
public static void main(String[] args)
{
String[] a = {"6", "s", "a", "b", "c", "s", "s", "s", "f", "s", "f", "s", "3", "3"};
String vString = Treearithmetic.find(a, "6", 14);
String vString2 = Treearithmetic.find2(a, "3", 14);
System.out.println(vString);
System.out.println(vString2);
}
// 找6 a b
public static String find(String[] a, String searchString, int nElement)
{
int lowerBound = 0;
int upperBound = nElement - 1;
while (true)
{
//int num=lowerBound+((upperBound-lowerBound)/2);后者超出232溢出成负数
int num = (lowerBound + upperBound) / 2;
if (a[num] == searchString)
{
return a[num];
}
else
{
upperBound = num - 1;
}
}
}
// 找3
public static String find2(String[] a, String searchString, int nElement)
{
int lowerBound = 0;
int upperBound = nElement - 1;
while (true)
{
int num = (lowerBound + upperBound) / 2;
if (a[num] == searchString)
{
return a[num];
}
else
{
lowerBound = num + 1;
}
}
}
}