How to Work Binary Search in Java : Java2021 - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

How to Work Binary Search in Java : Java2021

Simple Example of Binary Search

package Practice;
public class BinarySearch {
    public static void main(String[] args)
    {
        int arr[]={1,2,3,4,5,6,7,8,9,10};
        int number=10;
        int li=0;
        int hi=arr.length-1;
        int mi=(li+hi)/2;
        while (li<=mi){
            if (arr[mi]==number){
                System.out.println("item available at "+mi+" index position");
                break;
            }
            else if (arr[mi]<number){
                li=mi+1;
            }
            else {
                hi=mi-1;
            }
            mi=(li+hi)/2;
        }
        if (li>hi){
            System.out.println("Element not found");
        }
    }
}
Output : 

click to next :

Sorting and Searching Programs

Start with your Choice

-Important Programs for Freshers

Learn Pattern Programs 

Click to learn Array Programs

Click to Learn MySQL 

for any complaint regarding my Blog please visit contact us page and write what problem you have!

No comments:

Pages