package Text3;
//二分查找算法法
public class HalfSearch {
public static int halfSearch(int a[], int x) {
int left,mid ,right;
left = 0;
right = a.length - 1;
mid = (left + right) / 2;
while (a[mid] != x) {
if (x > a[mid]) {
left = mid + 1;
}
else if (x < a[mid]) {
right = mid - 1;
}
mid=(left+right)/2;
}
return mid;
}
public static void main(String[] args) {
int a[] = {2,4,6,8,10};
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
int s = 10;
int index = halfSearch(a, s);
System.out.println(s + "该数在数组中位置是第 " + (index+1)+"位");
}
}
package Text3;//二分查找算法法public class HalfSearch { public static int halfSearch(int a[], int x) { int left,mid ,right; left = 0; right = a.length - 1; mid = (left + right) / 2; while (a[mi