为什么但我输入大于等于78以上的整数时都会出现角标越界
import java.util.Arrays;
class A
{
public static void main(String[] args)
{
int[] arr = {40, 25, 68, 58, 77, 13, 9, 28, 63};
sort(arr);
String str = Arrays.toString(arr);
System.out.println(str);
int key = binarySearch(arr,45);
System.out.println(key);
}
public static void sort(int arr[])
{
for (int i = 0 ;i < arr.length-1 ;i++ )
{
int minIndex = getMinIndex(arr,i);
swap(arr,minIndex,i);
}
}
public static int getMinIndex(int[] arr,int index)
{
int minIndex = index;
for (int i = index+1;i < arr.length ;i++ )
{
if (arr[minIndex] > arr[i])
{
minIndex = i;
}
}
return minIndex;
}
public static void swap(int[] arr,int minIndex,int index)
{
int temp = arr[index];
arr[index] = arr[minIndex];
arr[minIndex] = temp;
}
public static int binarySearch(int[] arr,int key)
{
int begin = 0;
int end = arr.length;
int m;
while (begin <= end)
{
m = (begin + end) / 2;
if (key == arr[m])
{
return m;
}
if (key > arr[m])
{
begin = m + 1;
}else
{
end = m - 1;
}
}
return -1;
}
} |