Skip to main content

Java Program to search a number within the list using Binary Search method

import java.io.*;
import java.util.*;
public class Binary{
    Scanner scan=new Scanner(System.in);
    int beg,mid,last,flag=0,n;
    int list[]={1,3,5,7,8,9,13,15,19,24,27,29,30,33,35,36,39,43,47,51,53,60};
    public void input()throws IOException{
        System.out.print("Enter any number for Searching: ");
        n=scan.nextInt();
        compute(n);
    }

    public void compute(int n){
        beg=0;
        last=21;
        while(beg<=21){
            mid=(beg+last)/2;
            if(n>list[mid])
                beg=mid+1;
            else if(n<list[mid])
                last=mid-1;
            else{
                flag=1;
                break;
            }
        }
        if(flag==1)
            System.out.println("Search for "+n+" in the list is Successful!\nNumber's potision: "+(mid+1));
        else
            System.out.println("Search for "+n+" in the list is Unsuccessful!");
    }

    public static void main(String[] args)throws IOException{
        Binary obj=new Binary();
        obj.input();
    }
}

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...