Programming Hub C,C++,C#,Asp.Net,Ado.Net,Java,  HTML,SQL.

This Blog Post Only Education Purpose For All Education Related Blogs and Articles Post Here.Download Free Software and Study Materials Mores..

Wednesday, 21 December 2016

How To Write Binary Search In Java Program

Thank you Visiting In Advance BY IT PROGRAMMING WORLD

           How To Write Binary Search In Java Program

package binarysearch;
import java.util.*;
public class Binarysearch {
    public static void main(String[] args) {
        int []array=new int[10];
        int value=0;
        int index;
        System.out.println("Enter 10 Numbers");
        Scanner s=new Scanner(System.in);
        for(int i=0;i<array.length;i++)
        {
            array[i]=s.nextInt();        }
 
    System.out.println("Enter a Number to Saarch For:");
    value=s.nextInt();
    index=binarysearch(array,value);
    if(index!=-1)
    {
     System.out.println("Found at Index:"+index);  
    }
    else
    {
        System.out.println("Not Found :");
    }
    }
    static int binarysearch(int []search , int find)
    {
        int start,end,mid;
        start=0;
        end=search.length-1;
        while(start<=end)
        {
            mid=(start+end)/2;
            if(search[mid]==find)
            {
                return mid;
               
            }
            else if(search[mid]<find)
            {
                start=mid+1;
            }
            else
            {
                end=mid-1;
            }
            return -1;
        }
        return 0;
    }
}



No comments:

Post a Comment

Please Comment Your Valuable Feedback....

You Might Also Like

Developing Data-Centric Windows Applications using Java

Developing Data-Centric Windows Applications using Java Introduction The Core Java Programming and JDBC course provides an introd...

Unlimited Reseller Hosting