Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Binary search in Java #2093

Open
justdoit254 opened this issue Nov 22, 2021 · 2 comments
Open

Binary search in Java #2093

justdoit254 opened this issue Nov 22, 2021 · 2 comments

Comments

@justdoit254
Copy link

Algorithm and code of Binary search

@anesha1
Copy link

anesha1 commented Jan 20, 2022

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

@Sandyy07
Copy link

Sandyy07 commented Oct 3, 2022

Hi @abhpd , Kindly please assign me this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants