Java Tutorial

 
 
 
 

Image

import java.io.*;

class search
{
      String str;
      int key,size,seaArr[],bottom,top,middle;

      public void getdata()
      {
             System.out.print(\"Enter how many data you want to enter : \");
             System.out.flush();
             try{
                 BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
                 str=obj.readLine();
                 size=Integer.parseInt(str);
                 seaArr=new int[size];
                 System.out.println(\"\\nEnter Data in Sequential 

Order\");
                 for(int i=0;i<size;i++)
                   {
                       System.out.print(\"Enter element at \"+(i+1)+\"th 

position  :  \");
                       System.out.flush();
                       str=obj.readLine();
                       seaArr[i]=Integer.parseInt(str);
                   }
                }
            catch(Exception e)  {}
      }

      public int BinSrch()
      {
            System.out.println(\"=====BINARY SEARCH=====\\n\");
            getdata();
            System.out.print(\"\\nEnter Search Key : \");
            System.out.flush();
            try{
              BufferedReader obj=new BufferedReader(new InputStreamReader

(System.in));
              str=obj.readLine();
              key=Integer.parseInt(str);
              bottom=0;
              top=size-1;
                while(top>=bottom)
                    {
                         middle=(top+bottom)/2;
                        if(seaArr[middle]==key)
                           {
                                return(middle+1);
                           }
                        else
                          {
                                  if(seaArr[middle]>key)
                                        top=middle-1;
                                  else


bottom=middle+1;
                          }
                    }                        


                 }
             catch(Exception e) {}
             return(0);
        }    
}

class  BinSea
{
    public static void main(String args[]) 
    {
           search o1 = new search();
           int result;
           result=o1.BinSrch();
           if(result==0)
                 System.out.println(\"\\nSearch Not Found\");
            else
                 System.out.println(\"\\nSearch is Located at \"+result+\" 

Position\");
    }
}

    Related Post:
  1. Program to ask question and accept answer up to 3 attempt and display right answer if it is wrong

  2. An applet program that creates string dynamically and displays it

  3. An applet program to perform quick sort

  4. Program that will read real number from the keyboard and print the following output in one line: small Integer not less The Given Number Largest Int

  5. An applet program to display text

  6. Program to print a string on Standard Output Device

  7. An applet program to draw circle in center of the canvas

  8. Program to show an example of Driving and using a Class

  9. Program to display reverse $ tree

  10. Program to show the use of Do-While Loop

  11. Program to display triangle of numbers

  12. Pass the marks of 10 students from the client side design a servlet, which takes these marks and returns the number of students who obtained first cls

  13. Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

  14. program which takes a long sentence from the client passes to the server,server separates out each word and sends them back to client in reverse order

  15. Biological Sequence Allignment Using Dynamic Programing

  16. Program to show the use of Logical OR Operator (|) in If Statements

  17. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  18. An applet program of linear sort

  19. Program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7

  20. Program to declare, initialize and print a StringBuffer object

 
 
Didn't find what you were looking for? Find more on Program of binary search tree