0
我有這個,但該方法沒有顯示在bluej中創建的對象。 如何對int數組執行二分搜索然後輸出找到的int?如何在java中使用bluej實現二進制搜索?
public static int binarySearch(int a[], int element)
{
int first = 0;
int upto = a.length;
while (first < upto)
{
int mid = (first + upto)/2; // Compute mid point.
if (element < a[mid])
{
upto = mid; // repeat search in bottom half.
} else if (element > a[mid])
{
first = mid + 1; // Repeat search in top half.
}
else
{
return mid; // Found it. return position
}
}
return -(first + 1); // Failed to find key
}
說明的作用:如果(第一+高達)溢出的最大整數併成爲負數。 –