java-二分法查找数组元素
package com.rj.bd.lx; import java.util.Arrays; /** * @desc 数组元素的查找之二分法 * @author yhx * @time 2019-09-11 * */ public class ErFenFa { public static void main(String[] args) { int[] a = {123,235,45,6,7,9,95,4}; // 由于二分法适合已经排序好的数组所以先将数组进行排序 for (int i = 0; i < a.length-1; i++) { for (int j = 0; j < a.length-1-i; j++) { if(a[j+1]<a[j]){ int temp = a[j+1]; a[j+1] = a[j]; a[j] = temp; } } } System.out.println(Arrays.toString(a)); System.out.println(Search(a,6)); } private static int Search(int[] a, int value) { int low = 0; int high = a.length-1; while(low<high){ int mid=(low+high)/2; if(value==a[mid