给定未排序的数组A,大小为n,请先把数组排好序,然后用二分查找定数X
------解决方案--------------------
- Java code
int[] test = new int[] { 1, 6, 7, 9, 3, 5, 2 }; Arrays.sort(test); System.out.println(Arrays.toString(test)); int index = Arrays.binarySearch(test, 5); System.out.println(index);
------解决方案--------------------
楼上正解,此贴可结