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. File handling program to display directory contents

  2. RMI application for counting factorial of a given number.

  3. An applet program to display barchart of given data

  4. Program to show an example of creating Character Arrays from String objects

  5. Create an interface coversions. Which have the following functions Inchestomillimeters(), hptowatts(),wattstohp(),poundstogram(),gramtopounds()

  6. Program to show an example of Calling a Base Class Method from a Derived Class

  7. Program which will read a string and rewrite it in the alphabetical order

  8. Write a class whose objects holds a current value and have a method to add that value, printing the new value

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

  10. Program, which creates a three thread. One thread display the numbers from 1 to 5, second thread display the square root of that number etc

  11. Program to compare Strings by compring successive corresponding characters, starting with the first character in each String

  12. Program to show insertion of StringBuffer, String objects and other Basic Data Types into a StringBuffer object

  13. Program to perform Merge Sort

  14. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  15. Program to print triangle of numbers

  16. Program to display text

  17. Program to print some text on the same line using multiple print statements

  18. Program of an applet to illustrate BarChart

  19. Program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01

  20. Program of an applet to display alternate filled and empty circle

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