提示-二分查找:对折对折对折(先找中间,再找下一个中间)
前言
要求:要求数组元素有限而必须支持比较大小,滨崎数组中的元素已经按大小排序号。
1.案例理解
使用二分查找查找 int value =18;是否存在,如果存在显示下标,不存在不显示下标
数组:int[] arr = {2,5,7,8,10,15,18,20,22,25,28};//数组有序
package com.zpark.test;
public class Dichotomy {
public static void main(String[] args) {
int[] arr = {2,5,7,8,10,15,18,20,22,25,28};//数组有序
int value = 18;
int index = -1;
int left = 0;
int right = arr.length - 1;
int mid = (left + right)/2;
while (left<=right) {
//找到结束