package com.example.grokkingalgorithmsdemo.binarysearch;

import lombok.extern.slf4j.Slf4j;

/**
* @Author: Frank
* @Date: 2022-06-04 12:12
*/
@Slf4j
public class BinarySearchJ {

private static Integer binarySearch(int[] list,int item){
int low = 0;
int high = list.length-1;
if (item<list[low] || item>list[high]){
//if smaller than low or bigger than high,
//then meaning not the element in the collection.
log.info("not in this collection,return null");
return null;
}
//当两个下标还没有挨在一起或刚刚挨在一起时
while(low<=high){
log.info("start while~~");
//获取中间数
int mid = (low+high)/2;
//猜测列表中的这个中间数是否是要检索的数据
int guess = list[mid];
//如果是,直接返回
if(guess==item){
return mid;
}
//如果猜测的数字是大于传入的要检索的数字时,
//此时二分查找的策略是,既然中间数不是,那么就将包括中间数的所有大的数字都排除掉
if(guess>item){
high=mid-1;
}else{
//否则如果猜测的数小于传入的要检索的数字时,
//此时二分查找的策略则为,既然中间数不是,并且真正的数字大于现在猜测的中间数,
//那么肯定要提高现在的中间数,即将最小部分的数字提升到现中间数+1
low = mid+1;
}
}
return null;
}

/**
* 这些都建立在数据在排好序的情况下,如果不是排好序的情况,需要先进行排序
* 才可以执行二分查找
* @param args
*/
public static void main(String[] args) {
int[] myList = {1,3,5,7,9};

System.out.println(binarySearch(myList,3)); //1
System.out.println(binarySearch(myList,-1));//null
}
}

在二分查找算法中,有一个奇怪的地方,当给定要查找数字item,不在这个集合中,
也就是肯定大于最大值或小于集合最小值,那么肯定是不存在这集合中的。

当加入第15行判定之后,当给定数字-1被算法要求查找后(第55行代码),会进入if smaller than low or bigger than high的判定。

而如果不加入判定,计算是-1也会进入while循环并执行3次。