hacktoberfest2021 icon indicating copy to clipboard operation
hacktoberfest2021 copied to clipboard

Binary search in Java

Open justdoit254 opened this issue 4 years ago • 2 comments

Algorithm and code of Binary search

justdoit254 avatar Nov 22 '21 07:11 justdoit254

The algorithm for Binary search is quiet easy .Here it is: public int runBinarySearchIteratively( int[] sortedArray, int key, int low, int high) { int index = Integer.MAX_VALUE;

while (low <= high) {
    int mid = low  + ((high - low) / 2);
    if (sortedArray[mid] < key) {
        low = mid + 1;
    } else if (sortedArray[mid] > key) {
        high = mid - 1;
    } else if (sortedArray[mid] == key) {
        index = mid;
        break;
    }
}
return index;

}

Now if we implement the above algorithm into a code, that will be: BinarySearch_Java.docx

anesha1 avatar Jan 20 '22 14:01 anesha1

Hi @abhpd , Kindly please assign me this issue.

Sandyy07 avatar Oct 03 '22 17:10 Sandyy07