import java.util.Arrays; import java.util.Random; public class bulubulu { public static void main(String[] args) { int[] ints = new int[50]; for (int i = 0; i < ints.length ; i++) { ints[i]=new Random().nextInt(100); } int index = pp(ints,50); System.out.println("查找的数在索引为:"+index+"的位置"); } public static int pp(int[] ints,int i){ Arrays.sort(ints); int max = ints.length-1; int min = 0; while (min <= max){ int half = (min+max)/2; if (i == ints[half]) { return half; } if (i>ints[half]){ min = half+1; } if (i<ints[half]){ max = half-1; } } return -1; } }
二分查找法
最新推荐文章于 2022-02-07 14:26:03 发布