[Solved] Binary search code in java won’t run [closed]


The aim of the binary search is to find the index of a number inside a sorted array in a complexity log(n).


FIRST STEP : initialization

You start with your sorted array : {5,10,15,20,25,30,35,40,45,50}

small : is the current smallest index. At the beginning it should be 0 for a zero-based array.

big : is the current biggest index. At the beginning it should be the length of the array.


SECOND STEP : loop

while big >= small, you compute mid = (small + big) / 2.

  • If the value you are looking for is the one at the index mid, then you return true.

  • else if the value you are looking for is lower than the value at index mid, then you want to look a the lowest part of the array, so big becomes mid-1.

  • ELSE (and this is missing in your code) small becomes mid+1, because you want now to look on the highest part of the array.

If you go out of the loop, meaning that big is smaller than small, then you haven’t found the value, you return false.


FINNALY : you need to call the method binarySearch of a number inside your main method

2

solved Binary search code in java won’t run [closed]